What does the term execution context mean in JavaScript?
Think about where the code runs and what information it needs to run.
The execution context is the environment where JavaScript code is processed. It contains information like variables, functions, and the value of this. It is created whenever code runs.
What is the output of this JavaScript code?
function outer() { let a = 10; function inner() { let a = 20; console.log(a); } inner(); console.log(a); } outer();
Remember each function has its own execution context with its own variables.
The inner function has its own execution context where a is 20. The outer function's context has a as 10. So, inner() logs 20, then outer() logs 10.
What will be the value of result after running this code?
var x = 5; function test() { var x = 10; if (true) { let x = 20; } return x; } var result = test();
Look at the scope of each x and which one is returned.
The let x = 20 is block-scoped inside the if block and does not affect the var x = 10 in the function scope. The function returns the x declared with var, which is 10.
Which statement best describes the relationship between execution context and the call stack in JavaScript?
Think about how JavaScript manages running multiple functions.
The call stack is a data structure that keeps track of all execution contexts in the order they are created and removed. When a function runs, its execution context is pushed onto the stack; when it finishes, it is popped off.
What is the output of this JavaScript code?
console.log(a); var a = 10; function foo() { console.log(a); var a = 20; console.log(a); } foo(); console.log(a);
Remember how var variables are hoisted and initialized as undefined.
Before any code runs, var a is hoisted and initialized as undefined. So, the first console.log(a) prints undefined. Inside foo, var a is hoisted and initialized as undefined, so the first log inside foo is undefined. Then a is set to 20 and logged. Finally, outside foo, a is 10.