0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JavaScript]pushメソッドの返り値は新しい配列の要素数

Last updated at Posted at 2019-05-22

とあるfor文を書いていて、配列の要素を返したい時に、arr.push(i)をreturnしたらうまく返せなかったのでめも。

arr = []

for(let i = 10; i >= 0; i--) {
  arr.push(i)
}

console.log(arr)
// => [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

console.log(arr.push(11))
// => 12

上記のように、ES6でpushメソッドを使用した時の返り値は、新しい配列の要素ではなくて、新しい配列の要素数を返す。

参考
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/push

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?