 Square Root in JavaScript

# Square Root in JavaScript ## 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)); // 8
// Or
console.log(Math.sqrt(64)); // 8


## 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 = ;
console.log(Math.sqrt(num1)); // 8
// Or
console.log(Math.sqrt()); // 8


### 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)); // NaN
// Or
console.log(Math.sqrt([64, 4, 16])); // NaN


### 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)); // NaN
// Or
console.log(Math.sqrt("John")); // NaN


### Case 4: Square Root of a Negative Number

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

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


### 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)); // NaN
// Or
console.log(Math.sqrt({})); // NaN


### 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)); // 0
console.log(Math.sqrt(value2)); // 0
console.log(Math.sqrt(value3)); // 0


### 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)); // 8
console.log(Math.sqrt(-32 + -32)); // NaN

// Or
let x = 32;
console.log(Math.sqrt(32 + x)); // 8
console.log(Math.sqrt(-32 + -x)); // NaN


## 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)); // 2

// Or

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


## 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.

Last Updated: April 6th, 2023

Get tutorials, guides, and dev jobs in your inbox.

Joel OlawanleAuthor

Frontend Developer & Technical Writer Project

### React State Management with Redux and Redux-Toolkit

# javascript# React

Coordinating state and keeping components in sync can be tricky. If components rely on the same data but do not communicate with each other when...  Details

## Getting Started with AWS in Node.js

Build the foundation you'll need to provision, deploy, and run Node.js applications in the AWS cloud. Learn Lambda, EC2, S3, SQS, and more!