Math.sqrt()

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

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

Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN

Specifications

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

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ 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><mi>x</mi></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>
<pre class="brush:js">
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN</pre>
<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('ES5.1', '#sec-15.8.2.17', 'Math.sqrt')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-math.sqrt', 'Math.sqrt')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<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</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>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</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>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<p>&nbsp;</p>
Revert to this revision