Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ ES6 Destructuring | Getting Started: ES6
/
Introduction to React (Stage)

bookES6 Destructuring

メニューを表示するにはスワイプしてください

The older method of accessing and assigning values from an array was indexing:

const myArray = ['car', 'jet', 'spaceship'];
const landVehicle = myArray[0];
const airVehicle = myArray[1];
const spaceVehicle = myArray[2];

However, ES6 has introduced a new method called destructuring, which enables us to access and assign values from an array easily:

const [ landVehicle, airVehicle, spaceVehicle ] = myArray;
console.log (landVehicle);

The above method is much neater and requires less code.

We can also skip the elements we don't want to capture, for example, we can skip airVehicle but add an extra comma:

const [ landVehicle, , spaceVehicle ] = myArray;

Note

The const keyword is for defining constant terms, you can also use the let keyword in the Destructuring statement.

question-icon

Complete the following code:

function stringVariants (str) {
    return [str.toLowerCase (), str.toUpperCase ()];
}
[,] = stringVariants ("Hello World!");
console.log (lower, upper);
hello world! HELLO WORLD!

クリックまたはドラッグ`n`ドロップして空欄を埋めてください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  6

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  6
some-alt