Object Property


#1

I have a code like this:

let person = {
  name: 'Tyron',
  age: 40,
  weekendAlarm: 'No alarms needed',
  weekAlarm: 'Alarm set to 7AM'
};

console.log(person.name);
console.log(person.age);

let day = 'Tuesday';
let alarm;

if (day === 'Saturday' || day === 'Sunday' ) {
  alarm = person.weekendAlarm;
} else {
  alarm = person.weekAlarm;
}

console.log(person[alarm]);

When it’s run, there are three lines of result:

Tyron
40
undefined

For the first two lines, it makes sense for me since I try to access the name and age of the object person. However, for the last line, I wonder why it show undefined because I have already defined it in the if-else statement.


#2

That can be ignored since it is just the console telling us that console.log() has no return value.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.