JavaScript DESTRUCTURING in 8 minutes!

JavaScript DESTRUCTURING in 8 minutes!

Bro Code

6 месяцев назад

11,393 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@BroCodez
@BroCodez - 01.11.2023 05:06

// 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);

Ответить
@user-qj1lq4zx3p
@user-qj1lq4zx3p - 12.01.2024 17:26

Great job Bro Code! I became addicted to your tutorials. I admire the amount of work you invest in developing these learning materials.

Ответить
@shivanshuhere
@shivanshuhere - 28.12.2023 05:43

Smooth

Ответить
@shafiq_ramli
@shafiq_ramli - 29.11.2023 19:47

What about destructuring object inside an object? Hope you can make a video about that soon!

Ответить
@_Sahil-ru3lp
@_Sahil-ru3lp - 21.11.2023 07:31

The patrick in the object destruction example is me 😢😢

Ответить
@ghorlik432
@ghorlik432 - 20.11.2023 20:13

What about "this" or static in obj ?

Ответить
@mohsenmk2537
@mohsenmk2537 - 20.11.2023 19:01

😘thank you

Ответить
@tonytodd7011
@tonytodd7011 - 20.11.2023 18:27

Thanks Bro for your simple but great explanation!

Ответить
@xzex2609
@xzex2609 - 07.11.2023 20:51

can we use this or rest operator to deliver different amount of arguments like in python when we use (*args , **kwargs) ? destructuring is like unpacking , but is there such a feature in JS
I'm really having a hard time with the language coming from python , I wonder if it was better to learn Django

Ответить
@coder2010
@coder2010 - 06.11.2023 22:49

hey these are actually pretty useful tutorials, they touch more advanced topics than the other videos. are these old? or are they new videos? Thanks for everything bro ❤

Ответить