I've been learning javascript for a few months I've attend one js quiz I saw this questions I'm not able to understand the execution flow of this very simple code , It would be really helpful if someone will explain it to me clearly , Thanks in advance
var a = 'Is';
function test() {
var a = 'Fun';
function again() {
var a = 'JavaScript';
alert(a);
}
again();
alert(a);
}
test();
alert(a);
Output
Javascript
Fun
Is
-
1Did you try walking through the execution with a debugger?VLAZ– VLAZ2020年03月23日 12:37:23 +00:00Commented Mar 23, 2020 at 12:37
-
sorry, i haven't been tried a one actually, I've no idea what is a debugger,Could you please help me with any link to the debugger thing? thank you @VLAZHashim aslam– Hashim aslam2020年03月23日 12:42:40 +00:00Commented Mar 23, 2020 at 12:42
4 Answers 4
1: var a = 'Is';
2: function test() {
3: var a = 'Fun';
4: function again() {
5: var a = 'JavaScript';
6: alert(a);
7:
8: }
9: again();
10: alert(a);
11: }
12: test();
13: alert(a);
Before execution of Line 1: A variable a
, initialized with the value undefined
, and a function test
, are added to the current lexical environment. If this code is run in the global context, these variables will be added as properties on the global object.
Line 1: The string 'Is'
is assigned to the variable a
in this lexical environment.
Line 12: The hidden method [[Call]]
is invoked on the function test
, and a new execution context is created, with a variable a
(with an initial value of undefined
) and a function again
added to its lexical environment.
Line 3: The string 'Fun'
is assigned to the variable a
in this lexical environment.
Line 9: The hidden method [[Call]]
is invoked on the function again
, and a new execution context is created, with a variable a
(with an initial value of undefined
) added to its lexical environment.
Line 5: The string 'JavaScript'
is assigned to the variable a
in this lexical environment.
Line 6: The host-provided window.alert
function is invoked, passing the value associated with variable a
in this lexical environment ('JavaScript'
).
Line 10: The host-provided window.alert
function is invoked, passing the value associated with variable a
in this lexical environment ('Fun'
).
Line 13: The host-provided window.alert
function is invoked, passing the value associated with variable a
in this lexical environment ('Is'
).
-
1Thank you so much this really helps its more clear about how js works ,its easy to understand tooHashim aslam– Hashim aslam2020年03月23日 13:21:36 +00:00Commented Mar 23, 2020 at 13:21
It's all about variables scoping and closure.
Firstly test
is called, so script runs it.
Inside test
function, new function again
is created and called.
Every child function has access to parent functions variables (closure), but if the child function itself has a variable, that is overwriting parent's variable (var a
), than the child function uses it own variable (or variable that is in the closest closure).
For example:
const a = 1;
function first(){
const a = 2
console.log(a)
function(){
console.log(a)
}
}
will console log:
2
2
Some more nice info here: What is the scope of variables in JavaScript?
Hope with the help of console logs you will be able to understand the control flow.
Press Ctrl+Shift+I , run the code snippet at the end of this answer.
Now to navigate through the flow control use these buttons.
debugger
var a = 'Fun';
function test() {
console.log("Im in test");
var a = 'Is';
function again() {
console.log("Im in again");
var a = 'JavaScript';
alert(a);
}
again();
console.log("Iam here after calling again");
alert(a);
}
test();
console.log("Im here after calling test");
alert(a)
To begin with, refer to the following link on the alert() method https://www.w3schools.com/jsref/met_win_alert.asp. Besides, learn more about variables' scoping in Javascript on the following link https://www.w3schools.com/js/js_scope.asp.
As per your question, the execution flow is as follows:
- A Global Variable 'a' has been initialized or assigned to 'Is'
- We're creating a custom function test
- Inside our custom test() function, we create a local variable 'a' and Assign it to 'Fun'
- We create a local function again inside the test function
- Inside the again function, we create a local variable 'a' and assign its value to 'Javascript' and then echo an elert box with the value of 'a'
- Outside the again() function but inside test() scope we Invoke the function again and then we alert() the value of 'a' which the variable 'a' inside the test() and outside the again().
- Finally, outside the test() function we invoke the test() which then Invoke again inside it. We then alert the value of 'a'.
- Every time, we Invoke the test() function will trigger the Invocation of the again() and alert Inside it.
Hence, the output above. I hope the description will work.