44

I solved a kata on CodeWars and was looking through some of the other solutions when I came across the double asterisk to signify to the power of. I have done some research and can see that this is a valid operator in python but can see nothing about it in JavaScript documentation.

var findNb = m =>
{
  var n = Math.floor((4*m)**.25);
  var sum = x => (x*(x+1)/2)**2;
  return sum(n) == m ? n : -1;
}

Yet when I run this solution on CodeWars, it seems to work. I am wondering if this is new in ES6, although I have found nothing about it.

VLAZ
  • 18,437
  • 8
  • 35
  • 54
Ivan Gonzalez
  • 461
  • 1
  • 4
  • 4
  • 1
    `**` is the markdown indicator for bold. It was likely a syntax error by the author when writing the markdown answer. – Reactgular Oct 22 '15 at 15:19
  • 1
    from [spec](http://www.ecma-international.org/ecma-262/6.0/#sec-multiplicative-operators): _MultiplicativeOperator : one of * / %_ – Grundy Oct 22 '15 at 15:21
  • @Grundy he's also using the fat arrow, so its possible – r3wt Oct 22 '15 at 15:34
  • @r3wt, i not quite understand what you mean :-) link above to ES6 specification, where just three multiplicative operators – Grundy Oct 22 '15 at 16:34
  • 2
    The [fat arrow (=>)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) is in the ES6 spec. The [exponential operator (**)](https://github.com/rwaldron/exponentiation-operator) is in the ES7 spec. I don't know anything about codewars, but this could be valid JavaScript if you were using [Traceur](https://github.com/google/traceur-compiler) or [Babel](https://babeljs.io/) – Howard Renollet Oct 22 '15 at 17:10
  • 2
    So, I just looked at codewars, all of their code runs through BabelJS, which explains why this code works with ES6 and ES7 operators. – Howard Renollet Oct 22 '15 at 18:04

3 Answers3

60

Yes. ** is the exponentiation operator and is the equivalent of Math.pow.

It was introduced in ECMAScript 2016 (ES7).

For details, see the proposal and this chapter of Exploring ES2016.

Tomas Nikodym
  • 8,782
  • 3
  • 15
  • 17
23

** was introduced in ECMAScript 2016 (ES7). But keep in mind that not all javascripts environments implements it (for instance, Internet Explorer does not support it).

If you want to be cross browser, you have to use Math.pow.

Math.pow(4, 5)
Magus
  • 13,609
  • 2
  • 31
  • 47
  • 1
    ^^^ indeed, it will throw `Uncaught SyntaxError: Unexpected token *(…)` – taxicala Oct 22 '15 at 15:19
  • This answer depends which ECMAScript version you are using, because it's wrong for ES7 as Thomas pointed out – Luca Steeb Dec 20 '16 at 19:23
  • This answer may have been correct when it was posted, but it is now incorrect. It should be edited to mention the JavaScript versions in which `**` does/does not exist. – chris Apr 12 '17 at 13:13
4

** operator is a valid operator in ES7. It holds the same meaning as Math.pow(x,y) For example, 2**3 is same as Math.pow(2,3)

Here are the details from Wikipedia.

Two new features added in ES7:

the exponentiation operator (**) and Array.prototype.includes

https://en.wikipedia.org/wiki/ECMAScript#cite_ref-ES2016_12-1

You can play with this in this Babel Live compiler

Hari Das
  • 7,849
  • 6
  • 46
  • 53