Same name parameters in JavaScript
Now let the outer and inner functions have the same name parameters:
function test(num) {
function func(num) {
console.log(num); // shows 1
}
func(num);
};
test(1);
In this case, the inner function will have
the local variable num
. Its change
in the inner function will not affect the
outer variable num
:
function test(num) {
function func(num) {
num = 2; // changes the local variable num
}
func(num);
console.log(num); // shows 1 - nothing has changed
}
test(1);
It turns out that the inner function will
not be able to access the outer variable
num
in order to change it:
function test(num) {
function func(num) {
// here you can not access to the external variable num
}
func(num);
}
test(1);
Determine what will be output to the console without running the code:
function test(num) {
function func(num) {
console.log(num);
}
func(num);
}
test(1);
Determine what will be output to the console without running the code:
function test(num) {
function func(num) {
num = 2;
}
func(num);
console.log(num);
}
test(1);
Determine what will be output to the console without running the code:
function test(num) {
function func(num) {
console.log(num);
}
num = 2;
func(num);
}
test(1);
Determine what will be output to the console without running the code:
function test(num) {
function func(num) {
console.log(num);
}
func(num);
num = 2;
}
test(1);