Please support this book: buy it (PDF, EPUB, MOBI) or donate
4. Exponentiation operator (**)
The exponentiation operator (**
) is an ECMAScript 2016 feature by Rick Waldron.
4.1 Overview
> 6 ** 2
- 36
4.2 An infix operator for exponentiation
**
is an infix operator for exponentiation:
x
**
y
produces the same result as
Math
.
pow
(
x
,
y
)
4.3 Examples
Normal use:
const
squared
=
3
**
2
;
// 9
Exponentiation assignment operator:
let
num
=
3
;
num
**=
2
;
console
.
log
(
num
);
// 9
Using exponentiation in a function (Pythagorean theorem):
function
dist
(
x
,
y
)
{
return
Math
.
sqrt
(
x
**
2
+
y
**
2
);
}
4.4 Precedence
The exponentiation operator binds very strongly, more strongly than *
(which, in turn, binds more strongly than +
):
> 2**2 * 2
- 8
- > 2 ** (2*2)
- 16
4.5 Further reading
- Exponentiation Operator (Rick Waldron)
当前内容版权归 exploringjs.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 exploringjs.com .