Skip to content

Instantly share code, notes, and snippets.

@colintoh
Last active August 13, 2021 07:12
Show Gist options
  • Save colintoh/a65d51987922a4098278 to your computer and use it in GitHub Desktop.
Save colintoh/a65d51987922a4098278 to your computer and use it in GitHub Desktop.
Array Destructuring and Object Destructuring
//Without Array Destructuring
var nameArr = "Colin Toh".split(" ");
var firstName = nameArr[0],
lastName = nameArr[1];
console.log(firstName); // Colin
// With Array Destructuring
var [firstName,lastName] = nameArr;
console.log(firstName); //Colin
//Without Object Destructuring
var bookShelf = {totalBooks: 10, booksType: "detective"};
var totalBooks = bookShelf.totalBooks,
booksType = bookShelf.booksType;
console.log(totalBooks); // 10
//With Object Destructuring
var {totalBooks, booksType} = bookShelf;
console.log(totalBooks); //10
@colintoh
Copy link
Author

Line 23, remove space between var and {

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment