조건문
조건문(Conditional Statement)
주어진 조건에 따라서 에플리케이션을 다르게 동작하도록 하는 것으로, Boolean(true/false)은 조건문에서 핵심적인 역할을 담당
1. if 조건문
if (조건){
실행될 코드;
}
if(true){
alert(1);
alert(2);
alert(3);
alert(4);
}
alert(5); // 1 , 2 , 3 , 4 , 5
if(false){
alert(1);
alert(2);
alert(3);
alert(4);
}
alert(5); // 5
1-1. if ~ else
if(true){
alert(1);
} else {
alert(2);
} // 1
1-2. else if
if(false){
alert(1);
} else if(true){
alert(2);
} else if(true){
alert(3);
} else {
alert(4);
} // 2
2. 중첩 if 조건문
if(조건){
if(조건){
//코드 실행
}
}
id = prompt('아이디를 입력해주세요.');
if(id === 'Kelly'){
password = prompt('비밀번호를 입력해주세요.');
if(password === '111111'){
alert('인증 했습니다.');
} else {
alert('인증에 실패 했습니다.');
}
} else {
alert('인증에 실패 했습니다.');
}
Last updated
Was this helpful?