JavaScript的if语句可以让我们根据某些条件来执行不同的代码块。使用if语句的基本思路是将要执行的代码放在括号内,并使用if关键字进行匹配。下面是一些例子:
简单的if语句:
let age = 18;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are not an adult");
}
上述代码会输出“You are an adult”,因为年龄大于或等于18岁。
多条件if语句:
let day = 25;
let dayOfWeek = "Monday";
if (day === 22) {
console.log("It's Tuesday");
} else if (day < 22 || day === 22 && dayOfWeek !== "Saturday") {
console.log("It's still Monday");
} else {
console.log("It's Tuesday");
}
上述代码会输出“It’s Tuesday”,因为今天是星期二,而且星期二是不包括星期六的。如果星期六被认为是星期日,则输出“It’s still Monday”。
嵌套if语句:
let isStudent = true;
let day = 25;
if (isStudent && day >= 18) {
if (dayOfWeek === "Friday" || dayOfWeek === "Saturday" || dayOfWeek === "Sunday") {
isStudent = false;
} else {
console.log("You must be a student and weekday is valid");
}
} else {
console.log("You are not a student and weekday is invalid");
}
上述代码会输出“You must be a student and weekday is valid”,因为在日历上某一天周五或周六的晚上6点以后的某一天没有注册成为学生,输出该信息。在这种情况下,day变量为25,表示该晚上不在校。然后还可以通过条件执行如下的操作:弹出对话框询问“日历是否正常工作”以判断操作结果,输出到控制台“Please use weekdays instead”或其他结果,总之通过else-if或嵌套语句我们可以在基本if语句的基础上创造出复杂而有趣的条件语句。
大括号内的多条件判断:
let day = 25;
let dayOfWeek = "Monday";
let isStudent = true;
if (day === 22) {
if (dayOfWeek === "Friday" || dayOfWeek === "Saturday" || dayOfWeek === "Sunday") {
isStudent = false;
} else {
console.log("You must be a student and weekday is valid");
}
} else if (day < 22 || day === 22 && dayOfWeek !== "Saturday") {
console.log("It's still Monday");
} else {
console.log("It's Tuesday");
}
上述代码与第3个例子的代码效果相同,但是使用了大括号来明确代码块的结束位置。这种写法更加清晰易懂。
多个条件可以用花括号括起来的写法:
let day = 25;
let dayOfWeek = "Monday";
let isStudent = true;
if ((day >= 18 && day <= 22) || dayOfWeek === "Friday" || dayOfWeek === "Saturday" || dayOfWeek === "Sunday") {
isStudent = false;
} else {
console.log("You must be a student and weekday is valid");
}
上述代码将多个条件放在一对花括号中,使用逗号分隔。这种写法也更加清晰易懂。
使用逻辑运算符进行多条件判断:
let day = 25;
let dayOfWeek = "Monday";
let isStudent = true;
if (dayOfWeek === "Monday" || dayOfWeek === "Tuesday" || dayOfWeek === "Wednesday" || dayOfWeek === "Thursday" || dayOfWeek === "Friday" || dayOfWeek === "Saturday" || dayOfWeek === "Sunday") {
isStudent = false;
} else if (day >= 18 && day <= 22) {
console.log("You are a student");
} else if (day < 22 && day !== 22) {
console.log("You are not a student");
} else {
console.log("It's a Monday");
}
上述代码使用逻辑与运算符对多个条件进行判断。
在多条件if语句中使用switch语句:
let day = 25;
let dayOfWeek = "Monday";
let isStudent = true;
switch (day) {
case 22:
if (dayOfWeek === "Friday" || dayOfWeek === "Saturday" || dayOfWeek === "Sunday") {
isStudent = false;
} else {
console.log("You must be a student and weekday is valid");
}
break;
case 18:
if (dayOfWeek === "Friday" || dayOfWeek === "Saturday" || dayOfWeek === "Sunday") {
isStudent = false;
} else {
console.log("You are a student and weekday is valid");
}
break;
default:
console.log("Other weekday is invalid");
}
上述代码中使用了switch语句来判断day的值是否在18到22之间,并且根据dayOfWeek的值判断isStudent的值。
这些是JavaScript中if语句的一些基本用法,你可以根据自己的需求和喜好进行学习和使用。