0
0
Javascriptprogramming~20 mins

this in functions in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of this in functions
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of this in a regular function call
What is the output of the following code snippet?
Javascript
function show() {
  return this.value;
}

const value = 10;
console.log(show());
Aundefined
B10
CReferenceError
Dnull
Attempts:
2 left
💡 Hint
Think about what 'this' refers to in a regular function call in non-strict mode.
Predict Output
intermediate
2:00remaining
this inside an arrow function
What will be logged to the console when this code runs?
Javascript
const obj = {
  value: 42,
  arrowFunc: () => this.value
};
console.log(obj.arrowFunc());
Aundefined
BTypeError
C42
Dnull
Attempts:
2 left
💡 Hint
Remember how arrow functions handle 'this' differently than regular functions.
Predict Output
advanced
2:00remaining
this in method called with call()
What is the output of this code?
Javascript
const obj1 = { value: 5 };
const obj2 = { value: 10 };

function getValue() {
  return this.value;
}

console.log(getValue.call(obj2));
ATypeError
B10
Cundefined
D5
Attempts:
2 left
💡 Hint
The call() method sets 'this' explicitly.
Predict Output
advanced
2:00remaining
this in nested functions
What will this code output?
Javascript
const obj = {
  value: 100,
  outer: function() {
    function inner() {
      return this.value;
    }
    return inner();
  }
};

console.log(obj.outer());
A100
BTypeError
Cundefined
DReferenceError
Attempts:
2 left
💡 Hint
Consider what 'this' refers to inside the inner function.
🧠 Conceptual
expert
2:00remaining
Understanding this binding in event handlers
In the following code, what will be logged when the button is clicked?
Javascript
const button = document.createElement('button');
button.textContent = 'Click me';

const obj = {
  value: 50,
  handleClick: () => {
    console.log(this.value);
  }
};

button.addEventListener('click', obj.handleClick);
document.body.appendChild(button);
Anull
B50
CTypeError
Dundefined
Attempts:
2 left
💡 Hint
Think about how arrow functions capture 'this' and what 'this' refers to in this context.