>

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

Promises and Async JavaScript

Question 1 / 1

0:00
Core
Medium

A developer writes the following code to fetch and display the first color from an API:

let firstColor

fetchColors()
  .then(colors => {
    firstColor = colors[0]
    console.log('inside .then():', firstColor.title)  // logs correctly
  })

console.log('outside .then():', firstColor?.title)

The output is:

outside .then(): undefined
inside .then(): Rad Red

The developer is confused — the assignment firstColor = colors[0] clearly executes. Why does firstColor.title log as undefined outside .then()?