
const x = 1;
if (x >= 0) {
console.log('x는 0과 같거나 크다');
} else if (x > 0) {
console.log('x는 0보다 크다');
}
else-if를 Promise의 then 체이닝처럼 생각하는 경우가 있음
// Promise의 then 체이닝
.then.then.then.catch
// ➡️ 각각의 then이 순차적으로 실행됨
하지만 else-if는 완전히 다른 방식으로 동작함
const x = 1;
if (x >= 0) {
console.log('x는 0과 같거나 크다');
} else {
if (x > 0) {
console.log('x는 0보다 크다');
}
}
const x = 1;
// 👍 독립적인 조건문으로 분리
if (x >= 0) {
console.log('x는 0과 같거나 크다');
}
if (x > 0) {
console.log('x는 0보다 크다');
}
장점
const status = 'SUCCESS';
// 👍 명확한 switch-case 구문
switch(status) {
case 'SUCCESS':
handleSuccess();
break;
case 'ERROR':
handleError();
break;
case 'LOADING':
handleLoading();
break;
default:
handleDefault();
}
switch-case의 장점