JavaScript (ES2026) reference · Math Methods and Constants

JavaScript Math.sqrt() Method

Return the square root of a number, or NaN for negative numbers. Use it for distances and the Pythagorean theorem.

JavaScript (ES2026) Since ES1

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Math.sqrt(x)

Parameters

ParameterDescription
x A number.

Return value

A number or NaN.

Examples

Square rootsTry it yourself
console.log(Math.sqrt(16), Math.sqrt(2), Math.sqrt(-1));

Output

4 1.4142135623730951 NaN
Distance between two pointsTry it yourself
const dist = (a, b) => Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
console.log(dist({ x: 0, y: 0 }, { x: 3, y: 4 }));

Output

5

Version history

VersionChange
JavaScript ES1Added

Was this page helpful?