Reputation: 293
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
Upvotes: 0
Views: 590
Reputation: 3820
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)
Upvotes: 1
Reputation: 55729
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'
).
Upvotes: 2
Reputation: 36
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:
Hence, the output above. I hope the description will work.
Upvotes: 0
Reputation: 36
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?
Upvotes: 1