Premium Only Content

JavaScript DESTRUCTURING in 8 minutes! 💥
00:00:00 intro
00:00:21 swap the values of two variables
00:01:11 swap two elements in an array
00:02:34 assign array elements to variables
00:03:47 extract values from objects
00:06:11 destructuring in function parameters
// destructuring = extract values from arrays and objects,
// then assign them to variables in a convenient way
// [] = to perform array destructuring
// {} = to perform object destructuring
// --------- EXAMPLE 1 ---------
// SWAP THE VALUE OF TWO VARIABLES
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a);
console.log(b);
// --------- EXAMPLE 2 ---------
// SWAP 2 ELEMENTS IN AN ARRAY
const colors = ['red', 'green', 'blue', 'black', 'white'];
[colors[0], colors[4]] = [colors[4], colors[0]]
console.log(colors);
// --------- EXAMPLE 3 ---------
// ASSIGN ARRAY ELEMENTS TO VARIABLES
const [firstColor, secondColor, thirdColor, ...extraColors] = colors;
console.log(firstColor);
console.log(secondColor);
console.log(thirdColor);
console.log(extraColors);
// --------- EXAMPLE 4 ---------
// EXTRACT VALUES FROM OBJECTS
const person1 = {
firstName: 'Spongebob',
lastName: 'Squarepants',
age: 30,
job: "Fry cook",
};
const person2 = {
firstName: 'Patrick',
lastName: 'Star',
age: 34
};
const {firstName, lastName, age, job="Unemployed"} = person2;
console.log(firstName);
console.log(lastName);
console.log(age);
console.log(job);
// --------- EXAMPLE 5 ---------
// DESTRUCTURING IN FUNCTION PARAMETERS
function displayPerson({ firstName, lastName, age, job="Unemployed" }) {
console.log(`name: ${firstName} ${lastName}`);
console.log(`age: ${age}`);
console.log(`job: ${job}`);
}
displayPerson(person1);
displayPerson(person2);
-
2:39:38
TimcastIRL
5 hours agoMS-13 Story BURNS Democrats, Media PANICS Tries To Get Off Immigration Story | Timcast IRL
186K608 -
8:02:19
RalliedLIVE
11 hours ago $8.13 earnedWARZONE WINS ALL DAY w/ RAL
67.4K20 -
25:49
Stephen Gardner
7 hours ago🔴Trump Was RIGHT! Newsom’s HUMILIATING Report EXPOSED!
38.9K18 -
1:36:46
Tucker Carlson
5 hours agoThe Pentagon Didn’t Fire Dan Caldwell Over Leaks. They Fired Him for Opposing War With Iran.
122K139 -
LIVE
SpartakusLIVE
6 hours agoGames w/ StoneMountain64 || Duos w/ StevieT into the night
627 watching -
1:34:18
JustPearlyThings
5 hours agoHigh Value Men Are Happy With Wives That Are Mid (Call-in Show) | Pearl Daily
39.2K19 -
10:32:15
ttvglamourx
11 hours ago $1.99 earnedBIRTHDAY STREAM !DISCORD
26.4K4 -
LIVE
CHiLi XDD
6 hours ago[Macho Madness] Featuring G2G, Lanc & Mochi [ Fortnite --> Ghost Recon Wild Lands ]
121 watching -
2:17:35
We Like Shooting
17 hours ago $2.16 earnedWe Like Shooting 607 (Gun Podcast)
32.9K1 -
2:01:40
Joker Effect
3 hours agoYOU WOULD NEVER CLICK THIS BECAUSE YOU ARE SCARED YOU WILL LOVE IT.
27.9K2