MoreLesson 279Free preview
JS Exponentiation
This lesson is part of How To · All levels
Summary
The operator ** subtracts numbers in JavaScript, replacing the longer Math.pow designation. This operation has associativity to the right, so 2 ** 3 ** 2 is counted as 2 ** (3 ** 2). Be careful with the negative base: parentheses are necessary to get the desired result.
Key points
- A ** b means a is raised b, for example 2 ** 3 = 8.
- The operator ** is right-associative: 2 ** 3 ** 2 = 512.
- The negative base should be wrapped in parentheses: (-2) ** 2 = 4.
- Math.pow(a, b) is still valid, especially for old code or consistent styles.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.