offline version v3


⊗jsPmFVPNC 220 of 502 menu

Variable and parameter names coincidence in JavaScript

Function parameters are local variables within it. Let's name the function parameter with the same name as the external global variable:

function func(num) { console.log(num); } let num = 1; func(num);

In this case, it will turn out that there will be the variable num outside the function and variable num inside the function. But these will be different variables: changing a variable inside a function will change the local variable of the function. And the external variable inside the function will be inaccessible and cannot be changed in any way. Let's check:

function func(num) { num = 2; // change local variable } let num = 1; func(num); console.log(num); // shows 1 - nothing has changed

Determine what will be output to the console without running the code:

function func(num) { num = 2; } let num = 1; func(num); console.log(num);

Determine what will be output to the console without running the code:

function func() { num = 2; } let num = 1; func(); console.log(num);

Determine what will be output to the console without running the code:

function func() { let num = 2; } let num = 1; func(); console.log(num);
enru