Bird
0
0

Given the interface and code below, what will be the output of console.log(user.age)?

medium📝 component behavior Q13 of 15
Angular - TypeScript in Angular
Given the interface and code below, what will be the output of console.log(user.age)?
interface User {
  name: string;
  age: number;
}

const user: User = { name: 'Anna', age: 30 };
console.log(user.age);
Aundefined
BAnna
C30
DError: Property 'age' does not exist
Step-by-Step Solution
Solution:
  1. Step 1: Understand the interface and object

    The interface User requires name as string and age as number. The object user matches this with name: 'Anna' and age: 30.
  2. Step 2: Check the console.log output

    Logging user.age will output the number 30, as defined in the object.
  3. Final Answer:

    30 -> Option C
  4. Quick Check:

    Accessing user.age = 30 [OK]
Quick Trick: Interface ensures object has age; console logs that value [OK]
Common Mistakes:
  • Expecting name instead of age
  • Thinking interface adds runtime error
  • Assuming undefined because interface is only compile-time

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes