The Math.sqrt() method returns only the positive value. The sqrt() is a static method of Math, and it can be used without creating an object.
Math.sqrt
JavaScript Math.sqrt() function is a built-in function that is used to find the square root of any number. The square roots of any number are positive and negative, respectively. The sqrt() is the static method of Math, it can be used without creating an object.
To calculate a square root of a number in Javascript, use the Math.sqrt() function. The Math sqrt() is a built-in JavaScript method
Syntax
Math.sqrt(x)
Parameter(s)
The variable x, whose square root value is to be determined.
Return Value
The non-negative square root value.
See the following method.
Note
- If the argument is an array with a single element, the method returns the square root of that element.
- If the argument is an array with more than one element, the method returns NaN.
- If the passed value is not a valid number, the method returns the NaN.
- If a negative number is passed, a method returns NaN.
- If an argument is a negative infinity, then this method returns NaN.
- If an argument is a positive infinity, then this method returns positive infinity.
- If an argument is positive zero or negative zero, this method returns the same value passed.
If the passed value is null, the method returns 0.
Compatibility(Version and above):
- Google Chrome v1
- Firefox v1
- Edge v12
- Internet Explorer v3
- Opera v3
- Safari v1
- Android webview v1
- Chrome for Android v18
- Firefox for Android v4
- Opera for Android v10.1
- Safari on iOS v1
- Samsung Internet v1.0
- Node.js
JavaScript version: ECMAScript 1
Consider the following examples.
Javascript Math.sqrt() example
The following example demonstrates the use of the sqrt() method.
// app.js let a = 4; let b = 0; let c = 14; let d = [9]; // array with single element console.log(Math.sqrt(a)); console.log(Math.sqrt(b)); console.log(Math.sqrt(c)); console.log(Math.sqrt(d));
Output
node app 2 0 3.7416573867739413 3
Example 2
The following example demonstrates the cases where NaN is returned.
// app.js let a = "Hello, world"; //non-numeric string let b; //empty variable let c = -4; //negative number let d = [1, 2, 3, 4]; //array with more than one elements let e = {}; //empty object console.log(Math.sqrt(a)); console.log(Math.sqrt(b)); console.log(Math.sqrt(c));
Output
node app NaN NaN NaN NaN NaN
Example 3
The sqrt() method cannot be used with complex arguments as only integer arguments are accepted.
// Complex values cannot be passed as arguments as follows // since only integer arguments are accepted. console.log(Math.sqrt(2 + i));
Output
node app /Users/krunal/Desktop/code/node-examples/es/app.js:5 console.log(Math.sqrt(2 + i)); ^ ReferenceError: i is not defined at Object.<anonymous> (/Users/krunal/Desktop/code/node-examples/es/app.js:5:27) at Module._compile (internal/modules/cjs/loader.js:1128:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10) at Module.load (internal/modules/cjs/loader.js:983:32) at Function.Module._load (internal/modules/cjs/loader.js:891:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47
Example 4
The following example demonstrates the cases when positive infinity or negative infinity is passed as a parameter.
// app.js var a = Number.NEGATIVE_INFINITY; var b = Number.POSITIVE_INFINITY; console.log(Math.sqrt(a)); console.log(Math.sqrt(b));
Output
node example4 NaN Infinity
Example 5
The following example demonstrates the case of positive or negative zero being passed as a parameter.
// app.js let a = 0; let b = -0; console.log(Math.sqrt(a)); console.log(Math.sqrt(b));
Output
node example5 0 -0
Example 6
The following example demonstrates the cases where zero is returned.
// app.js let a = null; let b = ""; //empty string let c = []; //empty array console.log(Math.sqrt(a)); console.log(Math.sqrt(b)); console.log(Math.sqrt(c));
Output
node example6 0 0 0
A real-life example of Javascript sqrt() function
Given the two sides of a right-angled triangle, find the hypotenuse.
// Given two sides of a right-angled triangle, // find the hypotenuse. let side_1; let side_2; const r = require('readline'); const rl = r.createInterface({ input: process.stdin, output: process.stdout }); const prompt1 = () => { return new Promise((resolve, reject) => { rl.question('Side 1: ', (answer) => { side_1 = answer; resolve(); }); }); }; const prompt2 = () => { return new Promise((resolve, reject) => { rl.question('Side 2: ', (answer) => { side_2 = answer; resolve(); }); }); }; const main = async () => { await prompt1(); await prompt2(); rl.close(); let hypotenuse = Math.sqrt(side_1 * side_1 + side_2 * side_2); console.log("Hypotenuse: " + hypotenuse); } main();
Output
Test Case 1: ->node example7 Side 1: 3 Side 2: 4 Hypotenuse: 5 Test Case 2: ->node example7 Side 1: 15 Side 2: 8 Hypotenuse: 17