What is Math.sqrt() in JavaScript?

Share

Math is a built-in object in JavaScript that contains different methods and properties used to perform mathematical operations. It contains a sqrt() function, which is used to compute the square root of a specified number.

Syntax

Math.sqrt(param);

Parameter

  • param: This is the input value of the type Number, for which we want to find the sqrt().

Number in JavaScript is a 64-bit double-precision value which is the same as double in C# or Java.

Return value

  • Number: It returns the square root of the input parameter param. It is of type Number.

  • NaN: The function returns this if input parameter param < 0.

  • Infinity: The function returns this if input parameter param is Infinity.

Code

console.log("sqrt(-1) = " + Math.sqrt(-1));
console.log("sqrt(0) = " + Math.sqrt(0));
console.log("sqrt(16) = " + Math.sqrt(16));
console.log("sqrt(4.56) = " + Math.sqrt(4.56));
console.log("sqrt(-4.56) = " + Math.sqrt(-4.56));
console.log("sqrt(∞) = " + Math.sqrt(Infinity));