JavaScript Square Root operation

Posted on Jan 02, 2021

Finding Square Root number in JavaScript

To find the Square Root value of a certain number, you can use the JavaScript Math.sqrt() method. You need to pass the number into the method as follows:

// returns 3

If you pass a negative number to the method, it will return NaN instead of a number:

// returns NaN

The method only accepts one parameter. When you pass more than one parameter, the rest is ignored:

Math.sqrt(9, 18, 27);
// returns 3

Square Root constants in JavaScript

The Math object also has two static square root properties that you can use in your code immediately. They are:

  • Math.SQRT1_2 which returns the Square Root value of 1/2
  • Math.SQRT2 which returns the Square Root value of 2

Since both are static properties, you can use them just like a normal variable:

console.log(`The Square Root of 1/2 is ${Math.SQRT1_2}`);
console.log(`The Square Root of 2 is ${Math.SQRT2}`);

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.