时间:2022-07-16 09:42:24 | 栏目:JavaScript代码 | 点击:次
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);
/** * * Null、Undefined 布尔值等特殊值处理,使用||结合! * * */ 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