Author - StudySection Post Views - 32 views

Destructuring assignment – Javascript

Destructuring assignment:

It’s a Javascript expression that makes the assignment of the values of arrays or objects by unpacking them into new variables in an easier way.

  1. Array Destructuring in Javascript
    const dummyarr = [‘a’,’b’,’c’,’d’,8];
    let [val1,val2,val3,val4] = dummyarr;

    console.log(val1); => a
    console.log(val2); => b
    console.log(val3); => c

  2. Object Destructuring
    const person = {
    fname : “Abc”,
    lname: “Def”,
    age: 24
    let {fname,lname,age} = person;

    If you print these variables, they will return the values assigned to the person object.
    And if you want to use variable names different than those mentioned in the object properties, in that case, you will need to specify the variable name while declaring them otherwise it will return “undefined”.
    For example:
    let {fname:firstname,lastname,age} = person;
    console.log(firstname); => Abc
    console.log(lastname); => undefined
    console.log(age); => 24

So, the destructuring assignment makes it easier to assign the array or object values to different variables.

People having good knowledge of Financial accounting can get an Accounting Certification from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.

Leave a Reply

Your email address will not be published.