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

NodeJs - cross domain

Daily practice------There are 10 numbers that are required to be output from large to small by selection method

【指针内功修炼】函数指针 + 函数指针数组 + 回调函数(二)

js数组方法总结

2021年12月电子学会图形化四级编程题解析含答案:新冠疫苗接种系统

动态链接库.dll、.so和静态库.a,cmake指令

方舟开服教程win

Ark server opening tutorial win

并发编程的核心问题

How much does Ark Survival Evolved cost?
随机推荐
js中的基础知识点 —— 事件
自己悦表存心
问题4:什么是缺陷?你们公司缺陷的优先级是怎样划分的?
No inner demons, to dry!SQL optimization and diagnosis
爬虫注意
接口测试主要测试什么?
力扣1206. 设计跳表--SkipList跳表是怎么跳的?
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
gocron定时任务管理系统的安装与运行
高等数学(第七版)同济大学 习题4-1 个人解答
微电网和直流电网中最优潮流(OPF)的凸优化(Matlab代码实现)
开源一夏 | 阿里云物联网平台之极速体验
无内鬼,来点干货!SQL优化和诊断
问题1:get和post的区别
程序员面试必备PHP基础面试题 – 第十八天
rust编程基础
Use Typora+EasyBlogImageForTypora to write a blog and upload pictures quickly without a picture bed
又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
扫雷?拿来吧你(递归展开+坐标标记)
sql注入之报错注入(精简详细)