21
loading...
This website collects cookies to deliver better user experience
let arr = [1,2,3,4]
arr[2] //3
let obj = {
fname: 'john',
lname: 'doe'
}
obj.fname //john
//or
obj["fname"] //john
[a,b,c] = some_array
let arr = ["John", "Doe"]
// older way
let firstName = arr[0]
let lastName = arr[1]
console.log(firstName, lastName); //John Doe
//array destructuring way
let [firstName, lastName] = arr;
console.log(firstName, lastName); //John Doe
//what if there are more values in array
var [fname,lname] = ['john', 'doe', 'capscode', 26, car]
//we can use rest operator,
var [fname,lname, ...others] = ['john', 'doe', 'capscode', 26, car]
//Works with any iterables of JavaScript
String iterable
let [a, b, c] = "abc";
console.log(a) // "a"
console.log(b) // "b"
console.log(c) // "c"
let [first, ...rest] = "Hello";
console.log(first) // "H"
console.log(rest) // ["e", "l", "l", "o"]
//Swap variables trick using array destructuring
let guest = "Jane";
let admin = "Pete";
// Swap values: make guest=Pete, admin=Jane
[guest, admin] = [admin, guest];
let [firstName, surname] = [];
alert(firstName); // undefined
alert(surname); // undefined
so to get out of this,
// default values
let [name = "Guest", surname = "Anonymous"] = ["Julius"];
alert(name); // Julius (from array)
alert(surname); // Anonymous (default used)
const person = ['John', ["capscode", "google", "yahoo"], 'Doe'];
const [fname, [comp1, comp2, comp3]] = person;
console.log(comp3) //"yahoo"
let {var1, var2} = {var1:…, var2:…}
let person={
fname: "John",
lname: "Doe",
company: "capscode"
}
let {fname,lname,company } = person;
//we can also use rest operator
let {fname,...others} = person;
console.log(others)
console.log(others.lname)
console.log(others.company)
let fname, lname, company;
{fname, lname, company} = {
fname: "John",
lname: "Doe",
company: "capscode"
};
// error in this line
{.....}
in the main code flow (not inside another expression) as a code block. ( ... )
around the assignment statement are required when using object literal destructuring assignment without a declaration.{a, b} = {a: 1, b: 2}
is not valid stand-alone syntax, as the {a, b}
on the left-hand side is considered a block and not an object literal.// okay now
({fname, lname, company} = {
fname: "John",
lname: "Doe",
company: "capscode"
})
(....)
expression needs to be preceded by a semicolon or it may be used to execute a function on the previous line.let href, pathname
({href} = window.location)
({pathname} = window.location)
//TypeError: (intermediate value)(intermediate value) is not a function
let href
somefunc()
({href} = window.location)
//TypeError: somefunc(...) is not a function
()
after any ()
then put ;
after first ()
😅const student = {
name: 'John Doe',
age: 16,
scores: {
maths: 74,
english: 63
}
};
// We define 3 local variables: name, maths, science
const { name, scores: {maths, science = 50} } = student;
console.log(maths)//74
console.log(english)//undefined
const props = [
{ id: 1, name: 'John'},
{ id: 2, name: 'Saam'},
{ id: 3, name: 'Rahul'}
];
const [,, { name }] = props;
console.log(name); // "Rahul"
TypeError
let [NaN_] = NaN //TypeError: NaN is not iterable
let [Boolean_] = true // TypeError: true is not iterable
let [Number_] = 10 = TypeError: 10 is not iterable
let [NULL_] = nul //TypeError: null is not iterable
let [undefined_] = undefined //TypeError: undefined is not iterable
// NOTE: String are iterable in JavaScript
let [String_] = "CapsCode" // "C"