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

With a single operation, I improved the SQL execution efficiency by 10,000,000 times!

Js array method is summarized

简介undo log、truncate、以及undo log如何帮你回滚事物?

Ark server opening tutorial win

JS基础--判断

JS每晚24:00更新某方法

2021年12月电子学会图形化四级编程题解析含答案:棕熊大战

Internship Road: Documenting Confusion in My First Internship Project

证实了,百度没有快照了

leetcode:899. 有序队列【思维题】
随机推荐
How to prevent hacking Windows server security Settings
一对多查询(分页)
新版本的 MaxCompute 中,SQL支持的 LIMIT OFFSET 的语法是什么功能?
生物统计师与临床医生协同研究使用的低代码洞察平台丨数据科学 x 临床医学
2021年12月电子学会图形化二级编程题解析含答案:消灭蝙蝠
How much does Ark Survival Evolved cost?
Use Typora+EasyBlogImageForTypora to write a blog and upload pictures quickly without a picture bed
NodeJs - cross domain
王守创:多组学整合分析揭示植物代谢多样性的分子机制(8月2号晚)
Phaser(二):小恐龙跑酷游戏
liunx服务器遇到SYN_SENT洪水攻击
程序员面试必备PHP基础面试题 – 第十八天
JS基础--判断
劲爆!协程终于来了!线程即将是过去式
【周报】2022年7月24日
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
程序员面试必备PHP基础面试题 – 第二十一天
测试基础整合-测试分类、软件质量模型、测试流程、测试用例、测试点划分方法、缺陷、例子
夜神浏览器fiddler抓包
苹果开发「AI 建筑师」GAUDI:根据文本生成超逼真 3D 场景!