Math.sqrt()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Math/sqrt
  • Revision title: Math.sqrt()
  • Revision id: 482345
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment cleanup

Revision Content

{{JSRef("Global_Objects", "Math")}}

Summary

The Math.sqrt() function returns the square root (x) of a number.

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.

Specifications

Specification Status Comment
ECMAScript 1st Edition. Implemented in JavaScript 1.0 Standard Initial definition.
{{SpecName('ES6', '#sec-math.sqrt', 'Math.sqrt')}} {{Spec2('ES6')}} No changes.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

 

Revision Source

<div>
  {{JSRef("Global_Objects", "Math")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>Math.sqrt()</strong></code> function returns the square root (<math><msqrt>x</msqrt></math>) of a number.</p>
<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>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
    <tr>
      <td>ECMAScript 1st Edition. Implemented in JavaScript 1.0</td>
      <td>Standard</td>
      <td>Initial definition.</td>
    </tr>
    <tr>
      <td>{{SpecName('ES6', '#sec-math.sqrt', 'Math.sqrt')}}</td>
      <td>{{Spec2('ES6')}}</td>
      <td>No changes.</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision