Lycopersicum
Lycopersicum

Reputation: 569

Why does using exponentation operator ** usage produce error?

I've tried running node.js script with fragment:

const max = 2 ** 16;

and I ended up with error:

const max = 2 ** 8;
               ^

SyntaxError: Unexpected token *
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)

I imply that node.js should support exponent operator (**), because according to Node.js main page:

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

I also know, that there is built-in Math.pow() function, which does the job, however airbnb eslint style guide recommends using exponent operator (**):

Use exponentiation operator ** when calculating exponentiations. eslint: no-restricted-properties.

// bad
const binary = Math.pow(2, 10);

// good
const binary = 2 ** 10;

Therefore I imply, that using exponentiation operator ** may be system / installation of Node.js problem.


If it's relevant - I'm using Node.js v6.11.2 on Raspbian (armv7l)

Upvotes: 0

Views: 730

Answers (1)

Andreas
Andreas

Reputation: 21881

node.js supports the exponentiation operator since v6.7. But it requires the --harmony flag. Beginning with v7 the operator works out of the box.

http://node.green/#ES2016-features-exponentiation------operator

Upvotes: 2

Related Questions