双星号**是一个有效的Javascript运算符吗? [英] Is the double asterisk ** a valid Javascript operator?

查看:1371
本文介绍了双星号**是一个有效的Javascript运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码战中解决了一个kata,当我遇到双星号来表示其功能时,我正在查看其他一些解决方案。我做了一些研究,可以看到这是python中的一个有效的运算符,但在JavaScript文档中看不到它。

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;
}

然而,当我在代码大战上运行此解决方案时,它似乎有效。我想知道这是否是ES6中的新功能,虽然我没有发现它。

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.

推荐答案

是的。 ** 取幂运算符,相当于 Math.pow

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

它是在ECMAScript 2016(ES7)中引入的。

It was introduced in ECMAScript 2016 (ES7).

有关详细信息,请参阅提案探索ES2016 的https://leanpub.com/exploring-es2016-es2017/read#ch_exponentiation-operator>章节

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

这篇关于双星号**是一个有效的Javascript运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆