Destructuring assignment is a powerful feature in JavaScript that allows you to extract data from arrays and objects and assign it to variables. It is a concise and efficient way of writing code, and it makes it easy to work with data in JavaScript.
One of the most common use cases of destructuring is extracting values from an array. For example, instead of using the array index to access an item in an array, you can use destructuring to assign the values to variables.
const myArray = [1, 2, 3];
const [first, second, third] = myArray;
console.log(first); // 1
console.log(second); // 2
console.log(third); // 3
Destructuring can also be used to extract properties from an object.
const myObject = { name: "John", age: 30 };
const { name, age } = myObject;
console.log(name); // John
console.log(age); // 30
Another powerful feature of destructuring is the ability to use it in function parameters. This allows you to easily extract properties from an object or elements from an array and pass them as separate arguments to a function.
function myFunction({ name, age }) {
console.log(name);
console.log(age);
}
myFunction({ name: "John", age: 30 });
Destructuring also allows you to set default values for properties that do not exist in an object.
const myObject = { name: "John" };
const { name, age = 25 } = myObject;
console.log(name); // John
console.log(age); // 25
In summary, destructuring assignment is a powerful feature in JavaScript that can help you write more concise and efficient code. It allows you to easily extract data from arrays and objects, and use it in variable declarations and function parameters. With destructuring assignment,
Notes: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment