Top community members

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

[Edit]
EN

JavaScript - Math.sqrt() method example

13 points
Created by:
Ticket Ninja
2329

Math sqrt is static method that returns number which is square root of input value. The method works only on positive real numbers.

// ONLINE-RUNNER:browser;

console.log( Math.sqrt(  4   ) ); //  2
console.log( Math.sqrt(  9   ) ); //  3

console.log( Math.sqrt(  2   ) ); //  1.4142135623730951
console.log( Math.sqrt(  0.5 ) ); //  0.7071067811865476
console.log( Math.sqrt(  0   ) ); //  0
console.log( Math.sqrt( -1   ) ); //  NaN

1. Documentation

SyntaxMath.sqrt(number)
Parametersnumber - integer or float number value in range 0 to +Infinity (primitive value).
Result

Square root number value in range 0 to +Infinity (primitive value).

If operation can not be executed NaN is returned.

Descriptionsqrt is static method that returns number which is square root of input value. The method works only on positive real numbers.

2. Square root with Math.pow method examples

In this example the way how to calculate square root using power function is presented. 

// ONLINE-RUNNER:browser;

function calculateSqrt(value) {
    return Math.pow(value, 0.5);
}

// Examples:

console.log( calculateSqrt(  4   ) ); //  2
console.log( calculateSqrt(  9   ) ); //  3

console.log( calculateSqrt(  2   ) ); //  1.4142135623730951
console.log( calculateSqrt(  0.5 ) ); //  0.7071067811865476
console.log( calculateSqrt(  0   ) ); //  0
console.log( calculateSqrt( -1   ) ); //  NaN

See also

  1. JavaScript - how to calculate root of any degree?

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more