[ Web Proxy ]
URL:
Viewing: https://javascript.info/task/destruct-user [Back]  [Original]

Destructuring assignment
EN

We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

    Search on Javascript.info:
    Search in the tutorial:
    Light themeDark theme
    DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
    back to the lesson

    Destructuring assignment

    importance: 5

    We have an object:

    let user = {
      name: "John",
      years: 30
    };

    Write the destructuring assignment that reads:

    • name property into the variable name.
    • years property into the variable age.
    • isAdmin property into the variable isAdmin (false, if no such property)

    Heres an example of the values after your assignment:

    let user = { name: "John", years: 30 };
    
    // your code to the left side:
    // ... = user
    
    alert( name ); // John
    alert( age ); // 30
    alert( isAdmin ); // false
    solution
    let user = {
      name: "John",
      years: 30
    };
    
    let {name, years: age, isAdmin = false} = user;
    
    alert( name ); // John
    alert( age ); // 30
    alert( isAdmin ); // false

    Web Proxy Viewer  |  New URL  |  Original Page