Square each number in an array in javascript

后端 未结 14 1108
悲&欢浪女
悲&欢浪女 2020-12-16 08:36

I\'m trying to square each number in an array and my original code didn\'t work. I looked up another way to do it, but I\'d like to know WHY the original code didn\'t work.<

14条回答
  •  孤城傲影
    2020-12-16 08:56

    Here is the function write with ES6 Exponentiation (**):

    let arr = [1, 6, 7, 9];
    let result = arr.map(x => x ** 2);
    console.log(result);

提交回复
热议问题