Destructuring in Javascript

const [a,b,c] = [1,2,3]console.log(a) // 1console.log(b) // 2console.log(c) // 3
const [a,b,c] = [1,2]console.log(a) // 1console.log(b) // 2console.log(c) // undefined
const [a,b,c] = [1, ,3]console.log(a) // 1console.log(b) // undefinedconsole.log(c) // 3
const [firstName,lastName] = "Jordan Eckowitz".split(" ")console.log(firstName) // Jordanconsole.log(lastName) // Eckowitz
const [firstName,lastName,...rest] = ["Nelson","Mandela","President","South Africa"]console.log(firstName) // Nelsonconsole.log(lastName) // Mandelaconsole.log(rest) // [ 'President', 'South Africa' ]
const [username = "User", password = "Password"] = ["Jordan"]console.log(username) // Jordanconsole.log(password) // Password
const me = {firstName: "Jordan",lastName: "Eckowitz",city: "New York"}const {city,firstName,lastName} = meconsole.log(firstName) // Jordanconsole.log(lastName) // Eckowitzconsole.log(city) // New York
const me = {firstName: "Jordan",lastName: "Eckowitz",city: "New York"}const {city:place,firstName,lastName} = me
console.log(firstName) // Jordanconsole.log(lastName) // Eckowitzconsole.log(place) // New York
const me = {firstName: "Jordan",lastName: "Eckowitz",}const {city="Johannesburg",firstName,lastName} = meconsole.log(firstName) // Jordanconsole.log(lastName) // Eckowitzconsole.log(city) // Johannesburg
const me = {firstName: "Jordan",lastName: "Eckowitz",city: "New York"}const {city, ...rest} = me
console.log(city) // New York
console.log(rest) // { firstName: 'Jordan', lastName: 'Eckowitz' }

--

--

--

Full-Stack Web Developer | www.jordaneckowitz.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jordan Eckowitz

Jordan Eckowitz

Full-Stack Web Developer | www.jordaneckowitz.com

More from Medium

Promises In JAVASCRIPT

Comments in JavaScript

Rendering line breaks from JavaScript strings

JavaScript Eğitimi - 1 / Değişkenler, Operatörler ve Kontrol Yapıları