Math.sqrt()

  • Revision slug: JavaScript/Reference/Global_Objects/Math/sqrt
  • Revision title: sqrt
  • Revision id: 88055
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 6 words added, 11 words removed

Revision Content

 

Summary

Returns the square root of a number.

Method of Math
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 1st Edition

Syntax

Math.sqrt(x)

Parameters

x 
A number.

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.

Examples

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.

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Math/sqrt", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Math/sqrt", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Math/sqrt" } ) }}

Revision Source

<p>
 </p>
<h3 id="Summary" name="Summary"> Summary </h3>
<p>Returns the square root of a number.
</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Math"><code>Math</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.0</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 1st Edition</td> </tr> </tbody>
</table> <h3 id="Syntax" name="Syntax"> Syntax </h3>
<p><code>
Math.sqrt(<em>x</em>)
</code>
</p>
<h3 id="Parameters" name="Parameters"> Parameters </h3>
<dl><dt> <code>x</code> </dt><dd> A number.
</dd></dl>
<h3 id="Description" name="Description"> Description </h3>
<p>If the value of <code>number</code> is negative, <code>sqrt</code> returns <code>NaN</code>.
</p><p>Because <code>sqrt</code> is a static method of <code>Math</code>, you always use it as <code>Math.sqrt()</code>, rather than as a method of a <code>Math</code> object you created.
</p>
<h3 id="Examples" name="Examples"> Examples </h3>
<h4 id="Example:_Using_Math.sqrt" name="Example:_Using_Math.sqrt"> Example: Using <code>Math.sqrt</code> </h4>
<p>The following function returns the square root of the variable <code>x</code>:
</p>
<pre>function getRoot(x) {
   return Math.sqrt(x)
}
</pre>
<p>If you pass <code>getRoot</code> the value 9, it returns 3; if you pass it the value 2, it returns 1.414213562373095.
</p> {{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Math/sqrt", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Math/sqrt", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Math/sqrt" } ) }}
Revert to this revision