Object lesson chapter: 5

Why is the result of the code undefined?
I could not understand because I do not think to do a mistake.

let day = ‘Saturday’;

let alarm;

let person = {
name:‘robot’,
age:20,
weekendAlarm: ‘No alarms needed’,
weekAlarm: ‘Alarm set to 7AM’
};

if(day === ‘Saturday’ || day === ‘Sunday’) {
let alarm = ‘weekendAlarm’;
} else {
let alarm = ‘weekAlarm’;
}

console.log(person[alarm]);

My Output is undefined.

let gives block scope to variables. You have already declared the variable in global scope, so don’t use let inside the if statement.

Oh I got it. Thank you.

1 Like