Method #1

We can use the double-asterisk operator ** – it is “Exponentiation Operator”.

In an Exponentiation Expression, we have to specify the number we want to square to the left of the ** operator, and to the right of the ** operator, we have to write number 2, which stands for the second power (i.e. the square).

3**2
9

4**2
16

5**2
25

111111**2
12345654321

Method #2

To square a number in JavaScript, use the pow() method of the Math constructor.

The first parameter is the number to be squared. The second parameter is the number 2, which is the second power (square).

Math.pow(0, 2)
0
Math.pow(1, 2)
1
Math.pow(2, 2)
4
Math.pow(3, 2)
9
Math.pow(4, 2)
16

In the same way, you can take a number to the cube, and to any other degree.

Information references

ECMAScript Standard – Section “13.6 Exponentiation Operator” – https://tc39.es/ecma262/#sec-exp-operator

ECMAScript Standard – Section “21.3.2.26 Math.pow ( base, exponent )” – https://tc39.es/ecma262/#sec-math.pow

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like