Home  >  Q&A  >  body text

javascript - var nums = [1,8,9,3],num = nums.push(6);console.log(num);

为何打出来的是5?

黄舟黄舟2653 days ago536

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-11 10:35:38

    push返回的是个数

    reply
    0
  • 阿神

    阿神2017-04-11 10:35:38

    var nums = [1,8,9,3];
    nums.push(6); // push会影响数组自身, 而不是返回新的数组, splice也一样
    console.log(nums);
    // [1, 8, 9, 3, 6]

    reply
    0
  • PHPz

    PHPz2017-04-11 10:35:38

    var nums = [1,5,4,6];
      nums.push(7);
      num =nums;
      console.log(num);
    

    这时候输出的是一个数组,数组的长度是5

    reply
    0
  • Cancelreply