0
0
Javascriptprogramming~20 mins

this with arrow functions in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Arrow Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of this in arrow function inside object method
What is the output of the following code?
const obj = {
  value: 42,
  getValue: () => this.value
};
console.log(obj.getValue());
Javascript
const obj = {
  value: 42,
  getValue: () => this.value
};
console.log(obj.getValue());
Anull
B42
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Remember arrow functions do not have their own 'this'.
Predict Output
intermediate
2:00remaining
this in arrow function inside regular function
What will be logged by this code?
function Person() {
  this.age = 25;
  this.getAge = () => this.age;
}
const p = new Person();
console.log(p.getAge());
Javascript
function Person() {
  this.age = 25;
  this.getAge = () => this.age;
}
const p = new Person();
console.log(p.getAge());
ATypeError
B0
Cundefined
D25
Attempts:
2 left
💡 Hint
Arrow function inherits 'this' from the surrounding function.
Predict Output
advanced
2:00remaining
this in nested arrow functions
What is the output of this code?
const obj = {
  value: 10,
  method() {
    const arrow1 = () => {
      const arrow2 = () => this.value;
      return arrow2();
    };
    return arrow1();
  }
};
console.log(obj.method());
Javascript
const obj = {
  value: 10,
  method() {
    const arrow1 = () => {
      const arrow2 = () => this.value;
      return arrow2();
    };
    return arrow1();
  }
};
console.log(obj.method());
A10
Bundefined
CTypeError
D0
Attempts:
2 left
💡 Hint
Arrow functions inherit 'this' from the surrounding context.
Predict Output
advanced
2:00remaining
this in arrow function used as event handler
What will this code log when the button is clicked?
const button = document.createElement('button');
button.textContent = 'Click me';
document.body.appendChild(button);

button.addEventListener('click', () => {
  console.log(this);
});
Javascript
const button = document.createElement('button');
button.textContent = 'Click me';
document.body.appendChild(button);

button.addEventListener('click', () => {
  console.log(this);
});
Abutton element
BWindow object
Cundefined
DEvent object
Attempts:
2 left
💡 Hint
Arrow functions do not have their own 'this', so it uses the outer scope's 'this'.
🧠 Conceptual
expert
2:00remaining
Why arrow functions are not suitable as object methods
Why should arrow functions generally not be used as methods inside objects?

Choose the best explanation.
ABecause arrow functions do not have their own 'this', so 'this' inside them does not refer to the object itself.
BBecause arrow functions are slower than regular functions when used as methods.
CBecause arrow functions cannot access object properties at all.
DBecause arrow functions always return undefined when used as methods.
Attempts:
2 left
💡 Hint
Think about how 'this' behaves differently in arrow functions.