有区别
首先 多重 if else 比如
if (a) {
// a为true 执行
} else if (b) {
// b为true 执行
}
//这个语句是按照 if的顺序执行的 从上往下 只要遇见表达式为正确的 进入对应的代码区 后面的不执行
//但是举个栗子
var a = true ,b = true ,c =false
/*要求 我需要在 a为 true 的时候 输出 a正确, a和b都为true 的时候输出 a和b都正确
c为true 的时候 输出c正确 这样怎么做?
如果不使用嵌套*/
if (a) {
console.log("a正确")
} else if (a && b) {
console.log("a和b都正确")
} else if (c) {
console.log("c正确")
}
// 但是最终只会输出 a正确 因为代码走到第一步就不会往下走了
if (a) {
console.log("a正确")
if (a && b) {
console.log("a和b都正确")
}
} else if (c) {
console.log("c正确")
}
// 这样才能正确的输出 想要的结果