Destructuring function parameters in JavaScript. Code exampl... open

Destructuring function parameters in JavaScript. Code examples

If an array or an object is passed to a function as a parameter, then it can also be decomposed into separate values:

    function display({ name: userName, age: userAge }) {////indicate which specific parameters from the array we want to get and set variables to them
        console.log(userName, userAge);
    }

    function sum([a, b, c]) {//we indicate the elements of the array that we want to get, and the variables in which these elements will be written in order
        const result = a + b + c;
        console.log(result);
    }

    let user = {
        name: "Alexander",
        age: 33,
        email: "email@gmail.com"
    };

    let numbers = [3, 5, 7, 8];

    display(user);  // Alice 33 = name + age
    sum(numbers); //15 = 3 + 5 + 7
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation