0
1

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】分割代入について

Last updated at Posted at 2021-04-05

配列やオブジェクトの値を分割して代入したいときに有効

配列

sample.js
const array = [1, 2, 3];
// e0...array[0]...e1...array[1]...e2...array[2]............
const [e0, e1, e2] = array;
console.log(e0)// 1
console.log(e1)// 2
console.log(e2)// 3

オブジェクト

オブジェクトを変数に代入するときに分割して代入することができる

sample.js
const obj = {a: 10, b: 20, c: 30};
const {a, c} = obj;
console.log(a); // 10
console.log(c); // 30

動作確認

docker run -it --rm node:lts-alpine sh
/# vi test.js
# ファイルをコピペ
/# node test.js
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?