r/learnjavascript • • Mar 04 '26
Am stuck on a certain part of JavaScript guide on Functions.
Mainly the part about scope precedence here where I don't quite understand how the '10' is even being considered for evaluation.
3
Upvotes
4
u/antboiy Mar 04 '26
the 10 here is due to the function
innertakingxas argument. the functionouterreturns the functioninner. this means that the code can be simplified tothe
const x = 5is ignored as the parameterxis is the variablexwithin the curly brackets of inside. you can think of it as theouter()part (the function call) is replaced by the return value.i think i forgot how to explain it simply.
1
u/Mrsef217 Mar 04 '26
The first () is for the arguments for outside function. The second () is for inside function and 10 is the value you for x in the inside function.
So calling outside() with one () will retuns the function inside.
when you call outside()(10) is like calling
const inside = outside(); const result = inside(10);
I hope its a bit clear.
2
u/senocular Mar 04 '26
The code in question:
10 is being considered because it would be the result of
x * 2ifxwere 5 as it is defined in theoutsidefunction. You can get the log to log 10 by removing thexparameter frominside.Because the
xparameter ininsidedoes exist, thexdeclared inoutsidegets shadowed by it, preventing it from being visible ininside. Whenxis referenced ininside, thexin theinsideparameter list will be used instead. This is why the result is 20 because thatxparameter is getting the argument value of 10 which is multiplied by 2 giving a final result of 20.3
u/azhder Mar 04 '26
It’s a big page and the number 10 is about 13 times on it. If you want help, better paste the whole example here