Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript object destructing

const employee = {name: ‘ANE01’, email: ‘Anna@example.com’, phone:’0112–345–6789'};
//with destucturing
const {name, email, phone} = employee;
//without destucturing
const name = employee.name;
const email = employee.email;
const phone = employee.phone;
Comment

Array destructuring JS

// In an array destructuring from an array of length N specified on the right-hand side of the assignment, if the number of variables specified on the left-hand side of the assignment is greater than N, only the first N variables are assigned values. The values of the remaining variables will be undefined.

const foo = ['one', 'two'];

const [red, yellow, green, blue] = foo;
console.log(red); // "one"
console.log(yellow); // "two"
console.log(green); // undefined
console.log(blue);  //undefined
Comment

javascript destructuring

const [a, b] = array;
const [a, , b] = array;
const [a = aDefault, b] = array;
const [a, b, ...rest] = array;
const [a, , b, ...rest] = array;
const [a, b, ...{ pop, push }] = array;
const [a, b, ...[c, d]] = array;

const { a, b } = obj;
const { a: a1, b: b1 } = obj;
const { a: a1 = aDefault, b = bDefault } = obj;
const { a, b, ...rest } = obj;
const { a: a1, b: b1, ...rest } = obj;

let a, b, a1, b1, c, d, rest, pop, push;
[a, b] = array;
[a, , b] = array;
[a = aDefault, b] = array;
[a, b, ...rest] = array;
[a, , b, ...rest] = array;
[a, b, ...{ pop, push }] = array;
[a, b, ...[c, d]] = array;

({ a, b } = obj); // brackets are required
({ a: a1, b: b1 } = obj);
({ a: a1 = aDefault, b = bDefault } = obj);
({ a, b, ...rest } = obj);
({ a: a1, b: b1, ...rest } = obj);
Comment

js destructuring

let [person, age] = ['John', 26]
let msg = `${person} is ${age} years old`
console.log(msg)
Comment

Javascript array destructuring

let [a, b] = [9, 5]
[b, a] = [a, b]
console.log(a, b);
//Expected output: 5,9
Comment

javascript Array Destructuring

const arrValue = ['one', 'two', 'three'];

// destructuring assignment in arrays
const [x, y, z] = arrValue;

console.log(x); // one
console.log(y); // two
console.log(z); // three
Comment

javascript destructing

/*
* On the left-hand side, you decide which values to unpack from the right-hand
* side source variable.
* 
* This was introduced in ES6
*/
const x = [1, 2, 3, 4, 5]
const [a, b] = x
console.log(a) // prints out: 1
console.log(b) // prints out: 2
Comment

destructuring in javascript

/*
Array Destructuring: The following example shows us how to convert all 
the elements of an array to a variable.

Object Destructuring: The following example shows us how to convert all 
the properties of an object into a variable.
*/
//Array Destructuring
const friends = ['Bill', 'Gates'];
const [firstName, secondName] = friends;
console.log(secondName);  //Gates

//Object Destructuring
const person = {name: "Bill Gates", age: 17, phone: "123456789"};
const {name} = person;
console.log(name);  //Bill Gates
Comment

JavaScript Destructuring

// before you would do something like this
const person = {
    name: 'Sara',
    age: 25,
    gender: 'female'    
}

let name = person.name;
let age = person.age;
let gender = person.gender;

console.log(name); // Sara
console.log(age); // 25
console.log(gender); // female
Comment

destructuring js

console.log('Hello World') // Hello World

const {log} = console;
log('Hello World') // Hello World
Comment

destructuring javascript

let a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20

[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]

({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20

// Stage 4(finished) proposal
({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}
Comment

js destructuring explained

const { identifier } = expression;
Comment

js destructuring

/*simple example*/
const [name, age] = ["malek","25"];
Comment

Destructuring in javascript

const { name, age, job } = { name: 'abrar', age: 24, job: 'web-developer' }
console.log(name, age, job)
Comment

js destructuring

function calculatefood (food, tip) {
    tip_percent = tip / 100;
    tip_amount = food * tip_percent;
    total = food + tip_amount;
    return [tip_amount, total];
}
const [tip, sum] = calculatefood(100, 20);
console.log(tip);
console.log(sum);
Comment

PREVIOUS NEXT
Code Example
Javascript :: matches method in javascript 
Javascript :: how to target two id using jquery 
Javascript :: how to create new route in express 
Javascript :: if condition javascript 
Javascript :: chart js &php 
Javascript :: mongoose node js 
Javascript :: react-chartjs-2 
Javascript :: javascript symbols 
Javascript :: console log on html 
Javascript :: this in ajax call 
Javascript :: mongoose max record 
Javascript :: es6 spread assign nested object 
Javascript :: js print objects 
Javascript :: This function is used to store items in local storage 
Javascript :: what is next.js 
Javascript :: hydration in next js 
Javascript :: data attribute hide & show function syntax in jquery 
Javascript :: add webpack to react project 
Javascript :: javascript object methods 
Javascript :: target data option select vue js 
Javascript :: validator.js 
Javascript :: how to add css based on route react 
Javascript :: check if jwt token is valid 
Javascript :: skip map iteration javascript 
Javascript :: utc clock 
Javascript :: toast not showing 
Javascript :: discordjs 
Javascript :: prototype, __proto__ 
Javascript :: promise async await 
Javascript :: gsap react 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =