0
0
Javascriptprogramming~20 mins

Function execution context in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested function with variable shadowing

What is the output of the following JavaScript code?

Javascript
function outer() {
  let x = 10;
  function inner() {
    let x = 20;
    return x;
  }
  return inner();
}
console.log(outer());
A20
B10
Cundefined
DReferenceError
Attempts:
2 left
💡 Hint

Think about which x the inner function uses.

Predict Output
intermediate
2:00remaining
Value of 'this' inside a regular function

What will be logged to the console when this code runs?

Javascript
const obj = {
  value: 42,
  getValue: function() {
    return this.value;
  }
};
const getValue = obj.getValue;
console.log(getValue());
A42
Bnull
CTypeError
Dundefined
Attempts:
2 left
💡 Hint

Consider what this refers to when the function is called alone.

Predict Output
advanced
2:00remaining
Output of function with arguments object

What is the output of this code snippet?

Javascript
function test(a, b) {
  arguments[0] = 100;
  return a + b;
}
console.log(test(1, 2));
ATypeError
B3
C102
DNaN
Attempts:
2 left
💡 Hint

Remember how the arguments object relates to named parameters.

Predict Output
advanced
2:00remaining
Output of arrow function and 'this' binding

What will this code print?

Javascript
const obj = {
  value: 10,
  arrowFunc: () => this.value,
  regularFunc() {
    return this.value;
  }
};
console.log(obj.arrowFunc());
console.log(obj.regularFunc());
A10 and 10
Bundefined and 10
Cundefined and undefined
DTypeError and 10
Attempts:
2 left
💡 Hint

Think about how arrow functions handle this compared to regular functions.

🧠 Conceptual
expert
3:00remaining
Function execution context and closures

Consider this code. What will be the output and why?

Javascript
function createCounter() {
  let count = 0;
  return function() {
    count += 1;
    return count;
  };
}
const counter1 = createCounter();
const counter2 = createCounter();
console.log(counter1());
console.log(counter1());
console.log(counter2());
A1, 2, 1
B1, 1, 1
C0, 1, 0
D1, 2, 2
Attempts:
2 left
💡 Hint

Each call to createCounter creates a new closure with its own count.