let sale = true;
if(sale) {
console.log('Time to buy!');
}
let sale = true;
sale = false;
if(sale) {
console.log('Time to buy!');
} else {
console.log('Time to wait for a sale.');
}
// Output: Time to wait for a sale.
보다 작음: <
보다 큼: >
보다 작거나 같음: <=
보다 크거나 같음: >=
같음: ===
동일하지 않음:!==
let hungerLevel = 7;
if (hungerLevel > 7) {
console.log('Time to eat!');
} else {
console.log('We can eat later!');
}
let mood = 'sleepy';
let tirednessLevel = 6;
if (mood === 'sleepy' && tirednessLevel > 8) {
console.log('time to sleep')
} else {
console.log('not bed time yet')
}
//Output : not bed time yet
let mood = 'sleepy';
let tirednessLevel = 6;
if (mood === 'sleepy' || tirednessLevel > 8) {
console.log('time to sleep')
} else {
console.log('not bed time yet')
}
//Output : time to sleep
String or Number에서 참, 거짓을 판별할 때
값을 거짓으로 만드는 방법
value =
0
"" or ''
null
undefined
NaN
let isLocked = true;
isLocked ?
console.log('You will need a key to open the door.') :
console.log('You will not need a key to open the door.');
// Output: You will need a key to open the door.
let favoritePhrase = 'Love That!';
favoritePhrase === 'Love That!' ?
console.log('I love that!') :
console.log("I don't love that!");
//Output : I love that!
여러가지 가능한 결과를 얻을 수 있는 경우 사용
let season = 'summer';
if (season === 'spring') {
console.log('It\'s spring! The trees are budding!');
} else if (season === 'winter') {
console.log('It\'s winter! Everything is covered in snow.');
} else if (season === 'fall') {
console.log('It\'s fall! Leaves are falling!');
} else if (season === 'summer') {
console.log('It\'s sunny and warm because it\'s summer!')
} else {
console.log('Invalid season.');
}
//Output: It's sunnt and warm because it's summer!
여러가지 가능한 결과가 많을 경우 사용
let athleteFinalPosition = 'first place';
switch (athleteFinalPosition) {
case 'first place' :
console.log('You get the gold medal!')
break;
case 'second place' :
console.log('You get the silver medal!');
break;
case 'third place' :
console.log('You get the bronze medal!');
break;
default :
console.log('No medal awarded.');
break;
}