目录前言if else 基本使用简化if判断和优化代码单行if else使用&& || 优化使用三目运算符优化合并if使用includes 或者indexof使用sw
let name = "zhangsan";
if (name === "zhangsan") {
console.log("zhangsan"); // zhangsan
} else {
console.log("lisi");
}
if (name === "zhangsan") console.log("zhangsan");
else console.log("lisi");
let userInfo = {
name: "zhangsan",
};
if (userInfo.name === "zhangsan") {
console.log("zhangsan");
}
if (!userInfo.age) {
// userInfo.age--> false age属性不存在
console.log("年龄属性不存在");
}
userInfo.name === "zhangsan" && console.log("zhangsan");
userInfo.age || console.log("年龄属性不存在"); // 年龄属性不存在
let name = "zhangsan";
name === "zhangsan" ? console.log("zhangsan") : console.log("lisi");
let userObj = { name: "zhangsan", children: { name: "lisi" } };
if (userObj.children) {
if (userObj.children.name == "lisi") {
console.log(userObj.children.name);
}
}
if (userObj.children && userObj.children.name == "lisi")
console.log(userObj.children.name);
let age = "22";
if (age == "22" || age == "24" || age == "25") {
console.log(age);
}
let ageArray = ["22", "24", "25"];
if (ageArray.includes(age)) {
console.log(age);
}
if (ageArray.indexOf(age) > -1) {
console.log(age);
}
let val = "lisi";
switch (val) {
case "zhangsan":
console.log(val);
break;
case "lisi":
console.log(val);
break;
case "xiaoming":
console.log(val);
break;
case "xiaowang":
console.log(val);
break;
default:
console.log(val);
}
let userArray = [
{ name: "zhangsan" },
{ name: "lisi" },
{ name: "xiaoming" },
{ name: "xiaowang" },
];
// 多重if else
if (userArray.name === "zhangsan") {
console.log(userArray.name);
} else if (userArray.name === "lisi") {
console.log(userArray.name);
} else if (userArray.name === "xiaoming") {
console.log(userArray.name);
} else if (userArray.name === "xiaowang") {
console.log(userArray.name);
} else {
console.log("其他用户");
}
function getUser(username) {
return userArray.filter((item) => item.name == username)[0];
}
console.log(getUser("zhangsan"));
let filterUser = function (username) {
return `名字是${username}`;
};
let userMap = new Map([
["zhangsan", filterUser("zhangsan")],
["lisi", filterUser("lisi")],
["xiaoming", filterUser("xiaoming")],
["xiaowang", filterUser("xiaowang")],
]);
console.log(userMap.get("zhangsan")); // 名字是zhangsan
function userInfo(name, age) {
return `名字是:${name},年龄是:${age}`;
}
let userInfo = (name, age) => `名字是:${name},年龄是:${age}`;
console.log(userInfo("zhangsan", "23"));
function log1() {
console.log(1);
}
function log2() {
console.log(2);
}
let num = 2;
if (num === 1) {
log1();
} else {
log2();
}
num === 1 ? log1() : log2();
let getUser = (name = "zhangsan", age = 23) =>
`名字是:${name},年龄是:${age}`;
console.log("默认参数", getUser());
console.log("传参", getUser("lisi", 29));
let num1 = +"123";
console.log(num1);
let name1 = undefined;
let name2 = null;
let name3 = true;
let name4 = false;
let name5 = "lisi";
console.log(name1 || "其他人"); // 其他人
console.log(name2 || "其他人"); // 其他人
console.log(name3 || "其他人"); // true
console.log(!name3 || "其他人"); // 其他人
console.log(name4 || "其他人"); // 其他人
console.log(name5 || "其他人"); // lisi
console.log(!name5 || "其他人"); // 其他人
?.运算符在运算过程中,会首先从左到右执行,左侧的对象是否为null或undefined。如果是的,就不再往右边执行运算,而是返回undefined
更多链判断运算符请参考
https://developer.mozilla.org/zh-CN/docs/WEB/JavaScript/Reference/Operators/Optional_chaining
let user = {
name: "zhangsan",
children: { name: "lisi", children: { name: "xiaoming" } },
};
let name1 = user?.name;
let name2 = user?.children?.name;
let name3 = user?.children?.children?.name;
console.log(name1);
console.log(name2);
console.log(name3);
const foo = null ?? "default string";
const test = undefined ?? "default string";
const test1 = 123 ?? "default string";
console.log(test); // "default string"
console.log(foo); // "default string"
console.log(test1); // 123
const baz = 0 ?? 42;
console.log(baz); // 0
const str = "" ?? 42;
const str1 = "" || 42;
console.log(str); // ""
console.log(str1); // 42
码云 Https://gitee.com/lewyon/Vue-note
githup https://GitHub.com/akari16/vue-note
到此这篇关于JavaScript中if语句优化和部分语法糖小技巧推荐的文章就介绍到这了,更多相关js if语句优化内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 详解JavaScript中if语句优化和部分语法糖小技巧推荐
本文链接: https://lsjlt.com/news/148970.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0