Question
Is there a power operator in JavaScript?
Answer
There are a few ways we can do exponentiation in JavaScript.
- Using the exponentiation operator:
- Syntax:
baseNumber ** exponent
- Example:
2 ** 3 //output: 8
- Using the
Math.pow()
method:
- Syntax:
Math.pow(baseNumber, exponentToRaiseBaseNumber);
- Example:
Math.pow(7, 4); //output: 2401
- Multiplying the base number the exponent number of times:
- Syntax:
baseNum * baseNum * baseNum ...
- Example: Calculating 5 to the power of 4 -
5 * 5 * 5 * 5; //output: 625