Last active
August 13, 2021 07:12
-
-
Save colintoh/a65d51987922a4098278 to your computer and use it in GitHub Desktop.
Array Destructuring and Object Destructuring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍