본문 바로가기

Programming/JavaScript

Taking Decisions: if / else Statements

const age = 19;
const isOldEnough = age >= 18;

if (isOldEnough) {
    console.log('Sunny can start driving license');
}
// prints Sunny can start driving license
const age = 15;

if (age >= 18) {
    console.log('Sunny can start driving license');
} else {
    const yearsLeft = 18 - age;
    console.log(`Sunny is too young. Wait another ${yearsLeft} years :)`);
}
// prints Sunny is too young. Wait another 3 years :)
const birthYear = 1998;

let century;
if (birthYear <= 2000) {
    century = 20;
} else {
    century = 21;
}
console.log(century);
// prints 20

여기서 if문 밖에 century를 선언해줬다는 게 핵심!

if not, error message 뜸

Uncaught ReferenceError: century is not defined