Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the double asterisk ** a valid JavaScript operator?

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.

like image 595
Ivan Gonzalez Avatar asked Oct 22 '15 15:10

Ivan Gonzalez


People also ask

What is double asterisk in Javascript?

Double Asterisks (**) — Exponentiationpow(x, y) , which is equal to x^y. Many of you might have used Math. pow only when needed.

What does double asterisk mean in coding?

In a function definition, the double asterisk is also known **kwargs. They used to pass a keyword, variable-length argument dictionary to a function. The two asterisks (**) are the important element here, as the word kwargs is conventionally used, though not enforced by the language.

What is exponentiation operator?

The exponentiation operator ( ** ) returns the result of raising the first operand to the power of the second operand. It is equivalent to Math. pow , except it also accepts BigInts as operands.


2 Answers

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.

like image 74
Tomas Nikodym Avatar answered Sep 30 '22 22:09

Tomas Nikodym


** 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) 
like image 31
Magus Avatar answered Sep 30 '22 23:09

Magus