Languages
[Edit]
EN

ES7 / ES2016 / ECMAScript 2016 - Exponentiation operator (**) example

3 points
Created by:
Root-ssh
6322

In ECMAScript 2016 Exponentiation Operator (**) has been introduced. This article shows how to use the feature.

1. Exponentiation Operator example

// ONLINE-RUNNER:browser;

console.log(   1    **  2   ); // 1
console.log(   2    **  2   ); // 4
console.log(   3    **  2   ); // 9

console.log(   0    **  3   ); // 0
console.log(   0.5  **  0.3 ); // 0.8122523963562356
console.log( (-1)   **  4   ); // 1

console.log(   0    ** -0.4 ); // Infinity
console.log( (-0.5) ** -2   ); // 4
console.log( (-2.0) ** -2   ); // 0.25
console.log( (-2.0) **  0.5 ); // NaN

Note: for negative literal base values brackets must be used becase of syntax error, e.g.

  • (-2.0) ** 0.5 - correct syntax
  • -2.0 ** 0.5 - incorrect syntax

See also

  1. JavaScript - Math.pow() method example
Hey ūüĎč
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.