javascriptnode.jsfunction

hand over only some of the additional Variables for function


sorry for my terrible english

I have a funtion like this

function E(element1, element2 = "yes", element3="no")
{
console.log(`E1: ${element1}\nE2: ${element2}\nE3: ${element3}`)
}

When I call this function, I only wanna change the thrid element and the second can stay the same Something like this E("test", stay, "yes")

Thanks


Solution

  • See this answer for some help: Is there a way to provide named parameters in a function call in JavaScript?

    I rewrote your function as follows:

    //In ES2015, parameter destructuring can be used to simulate named parameters.
    
    function E(element1, {element2="yes", element3="no"}={}){
        console.log(`E1: ${element1}\nE2: ${element2}\nE3: ${element3}`);
    }
    
    // call E without a value for element2. The default is used.
    E("test", {element3: "foo"})
    

    Output:

    E1: test

    E2: yes

    E3: foo