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
'Programming > JavaScript' 카테고리의 다른 글
Type Conversion and Coercion (0) | 2022.08.21 |
---|---|
Coding Challenge #2 - BMI Comparison using if / else statements (0) | 2022.08.21 |
Strings and Template Literals (0) | 2022.08.09 |
Coding Challenge #1 - BMI Comparison (0) | 2022.07.10 |
Operator Precedence (0) | 2022.07.10 |