Explain exponentiation operator in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

Using Exponentiation operator we can find a number to the power of another number. It is denoted by **. We already have Math.pow() method to find a number to the power of another number. But the exponentiation operator(**) is common not only to javascript but also to other languages such as python, ruby, etc. 

Drawbacks of Exponentiation operator:

Its only drawback is that negative bases should be kept in parenthesis. If not error will be displayed.

Example-1

Live Demo

<html>
<body>
<script>
   var res1 = Math.pow(3,2)
   var res2 = (3) ** 2
   document.write(res1);
   document.write("</br>");
   document.write(res2);
</script>
</body>
</html>

Output

9
9

Example-2

In the following example, negative bases were used. In the case of Math.pow() there is no problem but when the exponentiation operator is used negative values should be placed in parenthesis. If not error will occur.

Live Demo

<html>
<body>
<script>
   var res1 = Math.pow(-3,2)
   var res2 = (-3) ** 2 // if parenthesis is not provided then error will occur.
   document.write(res1);
   document.write("</br>");
   document.write(res2);
</script>
</body>
</html>

Output

9
9
raja
Published on 05-Jul-2019 16:31:16
Advertisements