Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Math.sqrt: square root

Math.sqrt converts its argument to Number and returns its non-negative square root, or NaN for a negative value.

Version: ECMAScript 1 onward; example target: Node.js 24.22.2

Syntax

Math.sqrt(x)

Math.sqrt(x) applies ToNumber. For a positive finite value, it returns the Number representation of the mathematical square root; sqrt(9) is 3, while the result for 2 is a binary64 approximation of the irrational value sqrt(2). +Infinity remains Infinity.

+0 returns +0, and -0 returns -0, although ordinary display often hides the sign. A negative value other than -0, including -Infinity, produces NaN; so do NaN and a missing argument. A numeric string is converted, while a Symbol or BigInt throws TypeError.

The function has existed since ECMAScript 1 and is not a constructor. It is useful for lengths and distances, but prefer Math.hypot(a, b) over sqrt(a*a + b*b) to reduce intermediate overflow and underflow risk. Do not assume that squaring an approximated root always reproduces the input bit for bit.

Examples

console.log(Math.sqrt(9), Math.sqrt(2).toFixed(6));
console.log(Object.is(Math.sqrt(-0), -0), Math.sqrt(Infinity));
console.log(Number.isNaN(Math.sqrt(-1)), Number.isNaN(Math.sqrt()));
console.log(Math.sqrt("81"));
try { Math.sqrt(9n); } catch (error) { console.log(error instanceof TypeError); }

Result

3 1.414214
true Infinity
true true
9
true

Math.sqrt → 3, 1.414214, preserved -0, and errors

Sources