By executing the following code, we will get true twice in the console:

 var booltestvar = true; function testFunc1(){ otherFunc(booltestvar) } function otherFunc(booltestvar){ if (booltestvar === true){ console.log("true") booltestvar = false; } else {console.log("Уже не true")} } testFunc1(); testFunc1(); 

otherFunc does not need a parameter: booltestvar so visible inside it, since it is defined on the outer level. Therefore, if we remove the parameter from otherFunc , the second output to the kosol will be Уже не true .

Did I understand correctly that the value of a function parameter, if it exists, cannot be changed within the function itself and used when the function is called again? In any case, I ask you to comment on the results of the experiment.

    2 answers 2

    booltestvar inside function

     function otherFunc(booltestvar){ ... } 

    completely overlaps / hides the external variable. In your case, the parameter is passed to the function by value, changing it inside the function does not affect the value of the variable that was used in the function call.

    booltestvar inside function

     function otherFunc(){ ... } 

    this is an external variable, any changes are its immediate changes.

    Call the variables differently, and you will be happy.

    • Is this when objects suddenly began to be passed by value? - ThisMan
    • @vp_arth, I’m not talking about an example, but about a phrase A parameter is passed to a function by value , but an object, as a parameter, is passed to a function by reference - ThisMan
    • @vp_arth, in this context, the word Parameter looks like a generalized value, and not as a specific entity - ThisMan
    • @ThisMan objects are passed by value from the beginning. The fact that an object is a reference type does not affect the semantics of the parameter passing. - Pavel Mayorov

    If the question is about changing the value of a variable, you can take advantage of the fact that any function in this example is called in the context of a window . And since booltestvar can also be done in the same context, you can do so.

     var booltestvar = true; function testFunc1(){ otherFunc(booltestvar) } function otherFunc(booltestvar){ if (booltestvar === true){ console.log("true") this.booltestvar = false; } else {console.log("Уже не true")} } testFunc1(); testFunc1(); 

    But in any case, all the same, do not name the variables and the name of the parameters is the same