Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Math:sqrt

From MDC


Contents

[edit] Summary

Returns the square root of a number.

Method of Math

Static

Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

[edit] Syntax

Math.sqrt(x)

[edit] Parameters

x 
A number.

[edit] Description

If the value of number is negative, sqrt returns NaN.

Because sqrt is a static method of Math, you always use it as Math.sqrt(), rather than as a method of a Math object you created.

[edit] Examples

[edit] Example: Using Math.sqrt

The following function returns the square root of the variable x:

function getRoot(x) {
   return Math.sqrt(x)
}

If you pass getRoot the value 9, it returns 3; if you pass it the value 2, it returns 1.414213562373095.