Is the double asterisk ** a valid JavaScript operator?

JavascriptMathOperators

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

** 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)

Solution 3 - Javascript

** 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIvan GonzalezView Question on Stackoverflow
Solution 1 - JavascriptTomas NikodymView Answer on Stackoverflow
Solution 2 - JavascriptMagusView Answer on Stackoverflow
Solution 3 - JavascriptHari DasView Answer on Stackoverflow