if~else

else 문은 조건식을 만족할(true) 경우와 만족하지 않을(false) 경우에 따라 실행되는 코드가 달라집니다.

if~else문

if(조건식) { //참(true)일 때 실행 코드 } else { //거짓(false)일 때 실행 코드 }

//숫자 크기
let num = prompt("당신이 좋아하는 숫자는?");

if (num > 100) {
    document.write("숫자는 100보다 큽니다.");
} else {
    document.write("숫자는 100보다 작습니다.");
}
//짝수 홀수 구하기
let num2 = prompt("당신이 좋아하는 숫자는?");

if (num2 % 2 == 0) {
    document.write("짝수입니다.");
    alert("짝수임.");
} else {
    document.write("홀수입니다.");
    alert("홀수임.");
}

Last updated

Was this helpful?