JavaScript

How to Square a Number in JavaScript

A squared number is the same as a number multiplied by itself. JavaScript provides built-in methods as well as manual multiplication to square a number. The Math.pow() method and exponentiation operators give the facility for squaring a number. Moreover, users can extract the squared number by multiplying it by itself. In this guide, you will learn numerous methods of squaring a number in JavaScript. The content of this article serves as below:

  • Using Math.pow() Method
  • Using Exponentiation Operator
  • Multiplying Number by Itself

Method 1: Using the Math.pow() Method to Square a Number

JavaScript has built-in functions to compute the square of a number. For instance, the math object facilitates the pow() method to square a number. The method accepts two parameters; the first one contains the number and the second one represents the number of times to be multiplied. The syntax is provided below.

Syntax

Math.pow(a, b))

 

In this syntax, “a” represents the exponent and “b” represents the base. As we intend to use this method to get the square of a number, the value of the second argument “b” will be “2”:

Code

console.log("An example of Math.pow() method")
console.log(Math.pow(3, 2));

 

In this code, two values, “3” and “2,” are passed as arguments. The first value “3” represents the (base) number and the second value “2” (exponent) denotes the number of times 3 will be multiplied by itself:

Output

It is observed that the final output returns a “9”, which is the square of “3”.

Method 2: Using Exponentiation Operator for Squaring a Number

Another efficient method of squaring a number is possible through the exponentiation operator. It is represented as (**) two asterisks. The work of this method is similar to the pow() method in JavaScript.

The syntax for using the exponentiation operator is provided here:

Syntax

a ** b

 

The first number, “a” represents the value whose square is to be calculated. And the number “b” shows the number of times the number “a” is multiplied through itself.

Code

console.log("An example of exponential operator")
console.log(3 ** 2);

 

In this code, an exponentiation operator is employed to get the squared number.

Output

The output shows “9” in the console window, which is the square of “3”.  

Method 3: Multiplying Number by Itself to Square a Number

The simplest and most common method for getting the squared number is by multiplying the number with itself. In this way, the output returns the square of the input number. By considering, the code is written here.

Code

<td width="624">console.log("An example of multiplying number by itself");
let num = 3;
let sqNum = num * num;
console.log(sqNum);

 

In this code, the “num” variable is initialized with 3. After that, the number is multiplied by itself and stored in the “sqNum” variable. Finally, the console.log() method is utilized to display the squared number on the console:

Output

The execution of the above code returns the output “9” which is the square of the number “3”.

Conclusion

In JavaScript, Math.pow(), exponentiation operator, and multiplying a number by itself provide a squared number. The Math.pow() method and the exponentiation operator take two arguments as input. The first one is a base while the second one is an exponent. The base represents the number whose square is to be calculated, and the value of the exponent must be “2” to get the square. On the other hand, the user multiplies a number with itself to get a squared number. In this article, you have learned to square a number in JavaScript using three different methods alongside examples.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.