Use object and array destructuring
Table of contents
Object destructuring
Use object destructuring when accessing and using multiple properties of an object.
Time to write some facy code.
js
// badfunction getFullName(employee) {const firstName = employee.firstName;const lastName = employee.lastName;return `${firstName} ${lastName}`;}// goodfunction getFullName(employee) {const { firstName, lastName } = employee;return `${firstName} ${lastName}`;}// bestfunction getFullName({ firstName, lastName }) {return `${firstName} ${lastName}`;}
Array destructuring
js
const arr = [1, 2, 3, 4];// badconst first = arr[0];const second = arr[1];const forth = arr[3]// goodconst [first, second, ,forth] = arr;