정답:
let y = "Hello world"
if (y.length >= 5) {
console.log(y.length);
}
더 자세하게 만들어보았다.
let y = "Hello world"
if (y.length > 5) {
if (y.length = 5){
console.log('y의 길이는 5와 같다');
} else {
console.log('y의 길이는 5보다 크다');
}
}
이렇게 쓰면 안된다.
5보다 크다는 조건문을 썼는데 그 안에 또 5랑 같다는 조건문을 쓰면 결과가 똑바로 나오지 않는다.
if (y.length >= 5) {
console.log("y의 길이는 5이상이다");
} else {
console.log("y의 길이는 5미만이다");
}
이렇게 쓰자!
올바른 사용법
let age = 20;
let gender = "여성";
if (age >= 18) {
if (gender === "여성") {
console.log("성인 여성");
} else {
console.log("성인 남성");
}
} else {
if (gender === "여성") {
console.log("미성년 여성");
} else {
console.log("미성년 남성");
}
}
중첩문은 많이 쓰면 가독성이 떨어져 좋지 않다.
if 중첩문에는 같은 if 값을 넣으면 안된다!