코딩공부/HTML,CSS,JavaScript
JavaScript_조건문(if)
diary100
2023. 4. 5. 17:14
제어문1. if
if문
문법
if(조건식){
조건식의 결과가 true일 실행할 문장;
....
}
if~else 문
문법
if(조건식){
조건식의 결과가 true일 실행할 문장;
....
}else{
조건식의 결과가 false일때 실행할 문장;
....
}
if~else if~else 문
문법
if(조건식1){
조건식의 결과가 true일 실행할 문장;
....
}else if(조건식2){
조건식2의 결과가 true일때 실행할 문장;
....
}else if(조건식3){
조건식3의 결과가 true일때 실행할 문장;
....
}else{
모든 조건식이의 결과가 false일때 실행할 문장
}
HTML 예제 코드
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>if문</title>
</head>
<body>
<h2>if문</h2>
<script>
const age =Number(prompt('나이를 입력하세여'))
if(age>19){
console.log('성인입니다')
}
else if(age>14){
console.log('청소년입니다')
}else if(age>6){
console.log('어린이 입니다')
}
else {
console.log('유아 입니다')
}
</script>
</body>
</html>
결과