JavaScript

How to Find Square of a Number in JavaScript

In JavaScript, there are scenarios where math problems are involved, which include multiple calculations, complex equations, and taking measurements. For instance, while dealing with finance, normal distributions, measuring lengths and distances, quadratic formula (falling objects height), the radius of circles, and standard deviation in a JavaScript program. In such cases, finding the square of numbers can save your time and effort.

This blog will guide you to compute the square of the given number in JavaScript.

How to Find Square of a Number in JavaScript?

To find square of the given number in JavaScript, you can use:

We will now go through each of the mentioned methods one by one!

Method 1: Find Square of a Number in JavaScript Using Math.pow() Method

The “Math.pow()” method returns the value of “x” to the power of “y” like (x^y). It returns a “NaN” value in the case of a negative base and a non-integer base. Applying this method will assign a particular value as an exponent value and raise the base power accordingly.

Syntax

Math.pow(base, exponent)

Here, “pow()” refers to the power method, “base” refers to the value to be raised, and “exponent” will be raised according to the base value.

The following example explains the discussed concept.

Example

First, assign a specific value “5” to the variable named “squareNumber”:

var squareNumber= 5

Now, the “Math.pow()” method will raise the value of the assigned number to “2” as its power:

squareNumber= Math.pow(squareNumber,2)

Finally, display the resultant squared value:

console.log("The square of the given number is:", squareNumber)

The resultant output in this method will be as follows:

Method 2: Find Square of a Number in JavaScript Using Exponentiation Operator(**)

The “Exponentiation operator (**)” changes the value of the first variable with respect to the assigned second value, which will act as the power of the first variable. However, it also returns a “NaN” value under the same circumstances discussed in the previous method.

This operator can be utilized by adding (**) in between both the operand values, where the first value will act as a base and the operand value after the exponentiation operator (**) will be raised accordingly.

Syntax

x ** y

Here, “x” refers to the value of a variable, and “y” is the power value that will raise the value of x.

Go through the following example for demonstration

Example

First, assign a value “5” to the variable named “square”:

var square= 5

Next, apply the exponentiation operator “**” and specify “2” as the required power of the square variable:

squareNumber= square**2

Finally, display the squared number:

console.log("The square of the given number is:", squareNumber)

Output

Method 3: Find Square of a Number in JavaScript Using Custom Function

In JavaScript, a custom function can be utilized to get the required functionality. This technique can be used to create a function for finding the square of a number.

Overview the following example.

Example

Firstly, define a function named “squareNumber()” that accepts “square” as an argument and returns a value after multiplying the passed value with itself using the multiplication operator “*”:

function squareNumber(square){

return square*square

}

Next, invoke the squareNumber() custom function and pass “5” as an argument:

console.log("The square of the given number is:", squareNumber(5))

Output

We have provided the easiest methods for finding the square of a number in JavaScript.

Conclusion

To compute the square of a given number in JavaScript, the “Math.pow()” method is applied to assign a specific power to a particular value, and the Exponentiation operator “**” can be utilized to raise the power of the value(before the operator) with respect to the value(after the operator) or a custom function can be defined for the same purpose. This article provided a guideline for finding the square of a given number in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.