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
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”:
Now, the “Math.pow()” method will raise the value of the assigned number to “2” as its power:
Finally, display the resultant squared value:
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
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”:
Next, apply the exponentiation operator “**” and specify “2” as the required power of the square variable:
Finally, display the squared number:
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 “*”:
return square*square
}
Next, invoke the squareNumber() custom function and pass “5” as an argument:
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.