1. if문
if(조건){
실행할내용
}
// y의 길이가 5보다 크거나 같으면 그 길이를 consol.log로 출력 (length 활용)
let y = "hello world";
if(y.length >= 5){
console.log(y.length);
}
y = "hello";
if(y.length >= 5){
console.log(y.length);
}
y = "he";
if(y.length >= 5){
console.log(y.length);
}
2. if - else 문
//if문에서 false일 때 실행되는 코드를 추가
let x2 = -3
if(x2 > 0){ // 조건이 ture인 경우 실행
// main logic #1
console.log("x는 양수입니다")
} else { // fales인 경우 실행
// main logic #2
console.log("x는 음수입니다")
3.else if 문
let x3 = 5
if (x3 = 0){
console.log("x3은 0입니다");
} else if (x3 <0) { // if가 fales인 경우 실행
console.log("x3은 음수입니다");
} else { // if와 else if 모두가 fales인 경우 실행
console.log("x3은 양수입니다");
}
else if는 중간에 여러번 들어갈 수 있다 그럼 if 부터조건이 true인 경우 실행, false인 경우 차례로 조건을 검색 모든 조건이 false인 경우 else를 실행한다 if 이후에 나오는 else if와 else는 생략가능하다
'오늘 뭐했냐 > 개발에 대한 주저리' 카테고리의 다른 글
조건문 응용 (0) | 2023.05.23 |
---|---|
조건문(switch) (0) | 2023.05.23 |
화살표 함수( 간단한 정리) (0) | 2023.05.22 |
스코프 ( 간단한 정리 ) (0) | 2023.05.22 |
함수 ( 간단한 정리 ) (0) | 2023.05.22 |