当前位置:网站首页>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 跳出判断.
边栏推荐
猜你喜欢

技术分享 | 接口自动化测试如何搞定 json 响应断言?

并发编程的核心问题

在北极都可以穿短袖了,温度飙升至32.5℃
![Detailed explanation of cloud hard disk EVS and how to use and avoid pits [HUAWEI CLOUD is simple and far]](/img/95/c05f184a6221fefaaa93beb9dccc33.png)
Detailed explanation of cloud hard disk EVS and how to use and avoid pits [HUAWEI CLOUD is simple and far]

6000 字+,帮你搞懂互联网架构演变历程!

南京一研究所回应招聘硕士保安:负责安全生产等,48人选1

开源一夏 | 阿里云物联网平台之极速体验

如何将二维空间先验注入到ViT中? UMA&港理工&阿里提出SP-ViT,为视觉Transformer学习2D空间先验知识!...

devops-3:Jenkins增加静态节点

QT之Mysql驱动
随机推荐
2021年12月电子学会图形化四级编程题解析含答案:新冠疫苗接种系统
HDU 1406 (完数)
Three key expectations for the crypto market in August Price moves north?Still expected to be in turmoil
Currency ATM: Solana Wallet Has Unknown Security Vulnerability, A Large Number Of Users' Digital Assets Are Stolen
未来无法预料
问题1:批量测试(正式测试)之前应该怎么做?
2021年12月电子学会图形化四级编程题解析含答案:森林运动会
苹果开发「AI 建筑师」GAUDI:根据文本生成超逼真 3D 场景!
Several methods of installing Mysql in Linux
实习路途:记录给我的第一个实习项目中的困惑
JS手写call apply bind (详细)(面试)
gocron定时任务管理系统的安装与运行
又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
SwiftUI SQLite教程之了解如何在 SwiftUI 中使用 SQLite 数据库并执行 CRUD 操作(教程含源码)
【重构map】【重构filter】【重构Some】【重构reduce方法】【重构flat函数】
无内鬼,来点干货!SQL优化和诊断
自定SvgIcon公用组件
程序员面试必备PHP基础面试题 – 第二十天
基于matlab的遥测信道的基本特性仿真分析
取消转义字符(r)