function doSomething(a) {
b = a + doSomethingElse(a * 2);
console.log(b * 3);
}
function doSomethingElse(a) {
return a - 1;
}
var b;
doSomething(2); //15
When you call doSomething(2);
you pass value 2
to your function doSomething(a)
so value of a inside this function is a = 2
.
Now you have b = a + doSomethingElse(a * 2)
with a = 2
it’s b = 2 + doSomethingElse(4)
correct ?
In your doSomethingElse(a)
function you pass value 4
so a = 4
inside that function. It returns a - 1
so if a = 4
it will return 3
.
Now back to b = 2 + doSomethingElse(4)
since doSomethingElse(4)
returns 3
the result is b = 2 + 3
.
In your next step you’re printing out b * 3
-> console.log(b * 3)
which results in 15
.
This is because values of a
are local within a functions scope, you can check this for reference:
0
solved need help to understand this situation [closed]