What is Math.sqrt() in JavaScript?
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 typeNumber, for which we want to find thesqrt().
Numberin JavaScript is a 64-bit double-precision value which is the same asdoublein C# or Java.
Return value
-
Number: It returns the square root of the input parameterparam. It is of typeNumber. -
NaN: The function returns this if input parameterparam< 0. -
Infinity: The function returns this if input parameterparamisInfinity.
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));