Skip to content

Square root in code

Unicode escapes, HTML entities, LaTeX — and the actual functions that compute a square root in JavaScript, Python, Excel, and half a dozen other languages.

Unicode escapes & HTML entities

√ is Unicode code point U+221A. Most languages let you drop it straight into a string literal using an escape sequence instead of pasting the raw character.

JavaScript / TypeScript
"\u221A" // √
Python
"\u221a"  # √
Java / C#
"\u221A" // √
HTML
√ or √
CSS
content: "\221A";

LaTeX

\sqrt renders a proper radical with a bar over its argument in LaTeX, MathJax, and KaTeX — add a bracketed number for anything past a square root.

LaTeX — square root
\sqrt{x}
LaTeX — cube root
\sqrt[3]{x}
LaTeX — nth root
\sqrt[n]{x}

Calculating a square root in code

An escape sequence just gets the symbol onto the page — it doesn't compute anything. Here's the actual function or formula for each language and its result on 25.

JavaScript
Math.sqrt(25);
// 5
Python
import math
math.sqrt(25)
# 5.0
Python (no import)
25 ** 0.5
# 5.0
Excel / Google Sheets
=SQRT(A1)
Java
Math.sqrt(25);
// 5.0
C / C++
#include <math.h>
sqrt(25);
// 5.0
PHP
sqrt(25);
// 5
SQL
SELECT SQRT(25);
-- 5
Swift
25.0.squareRoot()
// 5.0
R
sqrt(25)
# 5

Gotchas worth knowing

Negative numbers

Math.sqrt(-1) returns NaN in JavaScript and Java. Python's math.sqrt(-1) raises a ValueError instead of returning anything — wrap it in a try/except if the input might be negative, or use cmath.sqrt() to get a complex result.

Integer vs. float results

Math.sqrt() always returns a float/double, even for perfect squares — Math.sqrt(25) is 5, not an int. In statically typed languages like Java or C, remember to store the result in a double, not an int, or it truncates.

Precision

Floating-point square roots aren't always exact. sqrt(2) ** 2 can come out to 2.0000000000000004 rather than 2 in most languages — round before comparing for equality.

Just need the symbol itself?

The copy formats page has √ ready to paste as plain text, a Unicode code point, an alt code, or a spreadsheet formula — no code editor required.

Open copy formats

Frequently asked questions

What is the Unicode escape for the square root symbol in code?

In JavaScript, Java, and C# string literals, use \u221A (capital A). In Python, use \u221a (lowercase a works the same as uppercase — Python's escape is case-insensitive for the digits, but lowercase is the conventional style). Both resolve to the same character, √, which is Unicode code point U+221A.

How do I calculate a square root in JavaScript?

Use Math.sqrt(x) — for example Math.sqrt(25) returns 5. It returns NaN for negative numbers since JavaScript's Math object doesn't handle complex numbers.

How do I calculate a square root in Python?

Import the math module and call math.sqrt(x), e.g. math.sqrt(25) returns 5.0. You can also use the exponent operator, 25 ** 0.5, which needs no import but returns a float either way. For negative inputs, math.sqrt() raises a ValueError — use cmath.sqrt() if you need a complex result.

What's the Excel or Google Sheets formula for square root?

=SQRT(A1), replacing A1 with the cell or number you want rooted. Both Excel and Google Sheets use identical SQRT() syntax. Pasting the √ character into a cell only displays it — it doesn't compute anything.

What's the LaTeX command for a square root?

\sqrt{x} renders a radical with a bar over x. For a cube root or other nth root, add a bracketed argument: \sqrt[3]{x} for cube root, \sqrt[n]{x} for an arbitrary root. Works in LaTeX, MathJax, and KaTeX.

Why does Math.sqrt(-1) return NaN instead of an error?

JavaScript's Math object only works with real numbers, and the square root of a negative number is imaginary. Rather than throwing, Math.sqrt() returns NaN (Not a Number) so a single bad value doesn't crash a larger calculation — check for it with Number.isNaN() if you need to handle it explicitly.