Bird
0
0

Given the code below, what will be the output of console.log(fullName()) after firstName.set('Jane') is called?

medium📝 component behavior Q4 of 15
Angular - Signals
Given the code below, what will be the output of console.log(fullName()) after firstName.set('Jane') is called?
const firstName = signal('John');
const lastName = signal('Doe');
const fullName = computed(() => `${firstName()} ${lastName()}`);
firstName.set('Jane');
console.log(fullName());
AThrows an error because fullName is not updated
B"Jane Doe"
C"Jane John"
D"John Doe"
Step-by-Step Solution
Solution:
  1. Step 1: Understand signal updates and computed recalculation

    Changing firstName triggers fullName to recalculate using the new firstName value.
  2. Step 2: Evaluate fullName after firstName.set('Jane')

    fullName returns "Jane Doe" because lastName remains "Doe" and firstName is updated.
  3. Final Answer:

    "Jane Doe" -> Option B
  4. Quick Check:

    Computed reflects updated dependencies = "Jane Doe" [OK]
Quick Trick: Computed signals auto-update on dependency changes [OK]
Common Mistakes:
  • Assuming fullName stays "John Doe" after firstName changes
  • Thinking computed signals do not update automatically
  • Expecting an error due to missing manual update

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes