Math.sqrt()

  • Revision slug: JavaScript/Reference/Global_Objects/Math/sqrt
  • Revision title: Math.sqrt
  • Revision id: 316361
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

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.

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>

<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-US/docs/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>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox"><code>Math.sqrt(<em>x</em>) </code></pre>

<h3 id="Parameters" name="Parameters">Parameters</h3>

<dl>
  <dt><code>x</code></dt>
  <dd>A number.</dd>
</dl>

<h2 id="Description" name="Description">Description</h2>

<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>

<h2 id="Examples" name="Examples">Examples</h2>

<h3 id="Example:_Using_Math.sqrt" name="Example:_Using_Math.sqrt">Example: Using <code>Math.sqrt</code></h3>

<p>The following function returns the square root of the variable <code>x</code>:</p>

<pre class="brush:js">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"
}) -->
Revert to this revision