5
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 3 years have passed since last update.

【JavaScript】unshift,shift,push,popの違い

Last updated at Posted at 2020-05-01

#【JavaScript】unshift,shift,push,popの違い

JavaScript初学者の視点で、備忘録兼ねて書きます。

配列を操作するunshift,shift,push,popの機能を整理してみました。

個人的に一番使うのはpush。
pushの反対はpop,shiftの反対はunshiftと覚えることで2つの暗記でまとめられます。

##unshift
配列の先頭に追加する

let a = [1,2,3];
let b = a.unshift(0);

console.log(a);//[0,1,2,3];
console.log(b);//4

##shift
配列の先頭から除外する

let a = [1,2,3];
let b = a.shift();

console.log(a);//[2,3];
console.log(b);//1

##push
配列末尾に追加する

let a = [1,2,3];
let b = a.push(4);

console.log(a);//[1,2,3,4];
console.log(b);//4

##pop
配列末尾から除外する

let a = [1,2,3];
let b = a.pop();

console.log(a);//[1,2];
console.log(b);//3
5
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
5
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?