Something Like that:
Scope Chain
For that you need to create first html code.
HTML
<p>Value of function a = <span id="demo1"></span></p>
After that add this JS code.
JS
//The Scope Chain function b(){ document.getElementById("demo1").innerHTML = myvar; } function a(){ var myvar = 2; b(); } var myvar = 1; a();
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”WErBLJ” default_tab=”result” user=”pradeepanvi”]See the Pen Scope Chain in Javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
Value of function a = 1
Why it’s showing function a = 1?
Because in the previous lecture you can see when var value is not declared so it’s given undefined!
Exactly this var value is not defined but there you can see in function b var is not defined as well. So now javascript finding parent until not find. and last It’s find outer function var value = 1.
That’s why It’s given us 1.