当前位置:网站首页>JS basics--judgment
JS basics--judgment
2022-08-03 15:32:00 【AsiaFT.】
JS基础--判断+ 循环
JS判断
if/else if/ else
三元运算符
简单的if else 书写,如果复杂的话,Don't write it as a ternary operator.If the statement is not executed after the judgment,则需要写null 或 undefined来占位
例:将如下if else The judgment turns into a ternary operator
let a = 10;
if (a > 0) {
if (a < 10) {
a++;
} else {
a--;
}
} else {
if (a > -10) {
a += 2;
}
}
由于最后一个if没有else 语句,所以需要加null 或者undefined进行赋值.转换后的结果为:
a > 0 ? (a < 10 ? a++ : a--) : (a > -10 ? a += 2 : null);
如果不加null或undefined,则报错:
Uncaught SyntaxError: Unexpected token ')'
switch case
- 每种caseBest to add it laterbrea.不加break,It does not matter whether the latter condition holds or not,都要执行,直到遇到break为止.
None of the conditions hold,则写default; - ** When a variable has several different values,The executed statement is the same as when,可以不加break;**
- 每种caseThe equal sign in is绝对相等(‘=’包括数值类型),和if else里面不同,if else里面是. For business rigor,使用===
例: 幂的表示: Math.pow(x,次方数)
let a = 10;
switch (a) {
case 1:
a += 1;
break;
case 5:
a += 5;
break;
default:
a = Math.pow(a, 2);
}
console.log(a);
//输出100
例: 如果将case1里的break去除,则为:
let a = 1;
switch (a) {
case 1:
a += 1;
case 5:
a += 5;
break;
default:
a = Math.pow(a, 2);
}
console.log(a);
输出7
case 1 执行,case 5 虽然不符合a=1, 但是由于case1 没有break,will also be executed.
当然,break Not adding is also useful.比方说,不同的取值,When corresponding to the same execution statement,就可以不加break;
例:
let a = 1;
switch (a) {
case 1:
case 5:
a++;
break;
default:
a = Math.pow(a, 2);
}
console.log(a);
// 2
a为1或5 时,做自增操作.因为case 1 里面没有内容,且没有break,所以继续执行5里面的内容,遇到break 跳出判断.
边栏推荐
猜你喜欢
随机推荐
JS每晚24:00更新某方法
6000 字+,帮你搞懂互联网架构演变历程!
如何用二分法搜索、查找旋转数组中是否含有某个(目标)值? leetcode 81.搜索旋转排序数组
有希望就是好的
南京一研究所回应招聘硕士保安:负责安全生产等,48人选1
使用虚幻引擎自动化工具实现自动化部署
Internship Road: Documenting Confusion in My First Internship Project
Clickhouse Filling the Pit 3: Left Join changed to Right Join, resulting in incorrect statistical results
ubiquant量化竞赛
冒烟测试冒烟测试
今日睡眠质量记录75分
问题10:注册页面的易用性测试?
方舟生存进化开服需要多少钱
分享一款免费OPC UA服务器
新一代网状网协议T-Mesh无线通信技术优势介绍
一个在浏览器中看到的透视Cell实现
MMA安装及使用优化
Ark server opening tutorial win
SwiftUI SQLite教程之了解如何在 SwiftUI 中使用 SQLite 数据库并执行 CRUD 操作(教程含源码)
测试基础整合-测试分类、软件质量模型、测试流程、测试用例、测试点划分方法、缺陷、例子