Method 1: Math.pow(x,y) function
console.log(Math.pow(-3, 3));
console.log(Math.pow(2, 4));
Method 2: Exponential operator **
(ECMAScript 2016 new)
alert( 2 ** 2 ); // 2² = 4
alert( 2 ** 3 ); // 2³ = 8
expand
Square root is a power operation with exponent ½:
alert( 4 ** (1/2) ); // 2(1/2 次方与平方根相同)
alert( 8 ** (1/3) ); // 2(1/3 次方与立方根相同)
Case for daffodils:
// 判断一个三位数是否是水仙花数:三位数用随机数生成
// 水仙花数的特点:这个数本身==这个数的个位数的立方+十位数的立方+百位数的立方
var num5 = ~~(Math.random() * (999 - 100 + 1) + 100);
console.log(num5);
// 456
var unit, ten, hun;//表示个十百
unit = num5 % 10;
ten = ~~(num5 / 10) % 10;
hun = ~~(num5 / 100);
console.log(hun, ten, unit);
console.log(num5 === unit ** 3 + ten ** 3 + hun ** 3 ? '水仙花数' : '不是水仙花数');
console.log('---------------------------');