>

CODEMASTERYLAB

IDE PLATFORM
CoursesCommunity BlogsKnowledge GraphQuizzesInterview Prep

Sign in to track progress & earn XP

Sign In Create Account
>
Code Mastery Lab

Code Mastery Lab

Sign In

Modern JavaScript You Need for React — Variables, Arrows, and Templates

Question 1 / 1

0:00
Core
Medium

A developer writes the following object:

const colorPicker = {
  selectedColor: null,
  pickColor: function(color) {
    setTimeout(function() {
      this.selectedColor = color
      console.log('Selected:', this.selectedColor)
    }, 100)
  }
}
colorPicker.pickColor('red')

After 100ms, the console logs 'Selected: undefined'. The developer replaces the setTimeout callback with an arrow function:

setTimeout(() => {
  this.selectedColor = color
  console.log('Selected:', this.selectedColor)
}, 100)

What does the console log now, and why?