20.2 C
New York

Square Root in JavaScript

Date:

Introduction

When developing mathematical solutions, answering algorithm questions, or attempting to solve a problem, situations may arise that require us to find the square root of specific values in JavaScript.

In this article, we will learn how to find the square root of a value or element in JavaScript.

We can use the Math.sqrt() static function in JavaScript to calculate the square root of any number. This function can be used to calculate the square root of a positive number and an array with a single element. Almost all modern browsers support this.

How to Find a Square Root of a Number in JavaScript

Math.sqrt() takes in a variable/value and returns its square root, if it’s a number. Let’s find the square root of 64 and log it into the console to illustrate the syntax of this static function:

let num1 = 64;
console.log(Math.sqrt(num1)); 

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

Some Common Errors and Accepted Values

In the following subsections, we’ll take a look at some interesting examples which will help us get a grasp on the values that can be passed as an argument of Math.sqrt() and the type of values that will throw errors.

Case 1: Square Root of an Array With One Element

Suppose the passed value is an array with a single element, Math.sqrt() returns the square root of that element:

let num1 = [64];
console.log(Math.sqrt(num1)); 

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

Case 2: Square Root of an Array With Multiple Elements

Suppose the value or argument passed is an array which has more than one element, then Math.sqrt() returns NaN:

let num1 = [64, 4, 16];
console.log(Math.sqrt(num1)); 

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

Case 3: Square Root of Non-number Value

Suppose the value passed is not a valid number, in this case, Math.sqrt() will return NaN:

let myValue = ["John"];
console.log(Math.sqrt(myValue)); 

console.log(Math.sqrt("John")); 

Case 4: Square Root of a Negative Number

We might accidentally pass a negative number as an argment of the Math.sqrt(), this will always return NaN:

let num1 = -64;
console.log(Math.sqrt(num1)); 

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

Case 5: Square Root of an Empty Object

Suppose we pass in an empty object, it will return NaN:

let value1 = {};
console.log(Math.sqrt(value1)); 

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

Case 6: Square Root in JavaScript Returns Zero

Suppose we pass in either an empty array, an empty string or null, this will always return zero:

let value1 = null;
let value2 = "";
let value3 = [];

console.log(Math.sqrt(value1)); 
console.log(Math.sqrt(value2)); 
console.log(Math.sqrt(value3)); 

Case 7: Square Root and Arithmetic Operations

It’s important to know that we can perform appropriate arithmetic operations inside Math.sqrt(). As long as the resulting value is valid, this will return the correct root value:

console.log(Math.sqrt(32 + 32)); 
console.log(Math.sqrt(-32 + -32)); 


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

Cube Root in JavaScript

JavaScript also provides the way of finding a cube root of a number. Say we need to find the cube root of the number 8 – we’ll use the Math.cbrt() method:

let num1 = 8;
console.log(Math.cbrt(num1)); 



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

Conclusion

In this article, we have seen the standard way to get the square root of a valid value with JavaScript, we have also seen possible errors and the values that will be returned. This will help us when writing our program and will make it easy for us to detect bugs.

  • Coinsmart. Europe’s Best Bitcoin and Crypto Exchange.Click Here
  • Platoblockchain. Web3 Metaverse Intelligence. Knowledge Amplified. Access Here.
  • Source: https://stackabuse.com/square-root-in-javascript/

This Post was originally published on Stackabuse

Related articles

spot_img

Recent articles

spot_img