发布时间:2023-04-01 12:00
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
码云 https://gitee.com/lewyon/vue-note
githup https://github.com/akari16/vue-note
到此这篇关于JavaScript中if语句优化和部分语法糖小技巧推荐的文章就介绍到这了,更多相关js if语句优化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
soloV2保姆级教程(含环境配置,训练自己的数据集,代码逻辑分析等。能踩得坑都踩了....)更新ing
【Java数据类型】“字符串”全解之“StringBuffer类和StringBuilder类”
《MySQL数据库》之"浅尝辄止"(五)DQL的使用详解和存储引擎简介
Vue2 Element DatePicker组件设置默认日期、控制日期范围
SpringBoot+SpringCloud+rabbitmq+redis(秒杀)
2022-2028年中国移动电商行业深度调研及投资前景预测报告
论文阅读笔记《Depth-attentional Features for Single-image Rain Removal》
第二十二:Python3+Selenium2+Pytest+Pytest-Html的UI自动化测试实战实例(二)