Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JavaScript Destructuring - From ES6

// assigning object attributes to variables
const person = {
    name: 'Sara',
    age: 25,
    gender: 'female'    
}

// destructuring assignment
let { name, age, gender } = person;

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

Destructuring assignment JS

const [firstElement, secondElement] = list;
// is equivalent to:
// const firstElement = list[0];
// const secondElement = list[1];

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
Comment

JavaScript Using es6(ES2015) Destructuring assignment

//JavaScript program to swap two variables

//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');

//using destructuring assignment
[a, b] = [b, a];

console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);
Comment

destructuring assignment in javascript

// destructuring assignment in javascript
// object destructuring
let person = { 
    name: "Chetan", 
    age: 30, 
    country: "India" 
};
const { name, age } = person;

console.log(name);
//expected output: "Chetan"

console.log(age);
//expected output: 30

console.log(country);
//expected output: Uncaught ReferenceError: country is not defined

// Array destructuring
const num = [1,2,3];
const [one, two, three] = num;
console.log(one); // 1
console.log(two); // 2
console.log(three); // 3
Comment

PREVIOUS NEXT
Code Example
Javascript :: Use Dynamic Scales 
Javascript :: call the httpclient.get method called 
Javascript :: what does the symbol function do in javascript 
Javascript :: Recursion In A Class Function 
Javascript :: Hardhat config file multiple network 
Javascript :: returning the outliers javascript array 
Javascript :: path.join javascript one folder above 
Javascript :: aws cognito user pool and angular 
Javascript :: create object in jquery dynamically 
Javascript :: Control a progress bar for custom video player 
Javascript :: wp include js 
Javascript :: Backbone View Event 
Javascript :: Backbone Model Validation And Inheritance 
Javascript :: Javascript set control state none opposite 
Javascript :: country select dropdown javascript 
Javascript :: add even javascript 
Javascript :: c# to javascript object 
Javascript :: hide and show button react js 
Javascript :: electron npm start not working 
Javascript :: onhover 
Javascript :: js return 
Javascript :: moment format dates 
Javascript :: events js 
Javascript :: online convert python to javascript 
Javascript :: javascript neue zeilekill 
Javascript :: javascript type 
Javascript :: javascript Display Undeclared Variable 
Javascript :: javascript Undeclared objects are not allowed 
Javascript :: ejs split string 
Javascript :: jquery put value in table 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =