JavaScript Square Root

JavaScript Square Root | Situations requiring us to discover the square root of specific values in JavaScript may emerge when constructing numerical equations, addressing algorithm queries, or attempting to solve an issue. In this tutorial, we’ll learn how to find square root in JavaScript.

In JavaScript, we may utilize the Math.sqrt() static function to find the square root of any number. This function can be used to find the square root of a positive number or a single-element array. This is supported by almost all current browsers.

How to find square root in JavaScript?
If a variable or value is a number, Math.sqrt() returns its square root. To demonstrate the syntax of this static function, find the square root of 25 and log it into the console.

console.log(Math.sqrt(25));

Output:-

5

The output for the above code is 5. Thus the syntax for the square root function can be stated as:

Syntax:- Math.sqrt(value);

In the subheadings that continue, we’ll look at some noteworthy cases to help us understand the types of values that can be supplied as arguments to Math.sqrt() and the types of values that can cause errors. Let us see how to find square root in JavaScript.

JavaScript Square Root Example

Case 1: Square root of array (single value)
Math.sqrt() returns the square root of an array with a single element if the given value is an array with a single element.

console.log(Math.sqrt([4]));

Output:-

2

Case 2: Square root of an array (multiple values)
If the value or argument supplied is an array with several elements, Math.sqrt() will return NaN.

console.log(Math.sqrt([4, 9, 16]));

Output:-

NaN

Case 3: Non-numeric value
In the scenario where the value supplied is not a valid number, Math.sqrt() will return NaN.

console.log(Math.sqrt("Know Program"));

Output:-

NaN

Case 4: Square root of a negative number
If a negative value is supplied as an argument to Math.sqrt(), the result will always be NaN.

console.log(Math.sqrt(-100));

Output:-

NaN

Case 5: Outputs 0 when:
This will always return 0 if we give in an empty array, an empty string, or null.

console.log(Math.sqrt(null));
console.log(Math.sqrt([]));
console.log(Math.sqrt(''));

Output:-

0
0
0

Case 6: Square root in JavaScript of an empty object
If we pass in an empty object, the result will be NaN.

console.log(Math.sqrt({ }));

Output:-

NaN

Case 7: Arithmetic operations and square roots
It’s critical to understand that we can use Math.sqrt() to execute acceptable arithmetic operations. This will return as long as the resulting value is legitimate.

console.log(Math.sqrt(50 + 50));
console.log(Math.sqrt(-2 + -2));

let x = 50;
console.log(Math.sqrt(50 + x));
console.log(Math.sqrt(-2 + -x)); 

Output:-

10
NaN
10
NaN

We saw the typical technique to get the square root of a valid value with JavaScript, as well as various errors and the values that will be returned, in this post. This will aid us in the development of our code and make it easier for us to discover bugs.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *