1. 运算符
1.1 算术运算符
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<meta http
-equiv
="X-UA-Compatible" content
="ie=edge">
<title
>Document
</title
>
<script
>
console
.log(1 + 1);
console
.log(1 - 1);
console
.log(1 * 1);
console
.log(1 / 1);
console
.log(4 % 2);
console
.log(5 % 3);
console
.log(3 % 5);
console
.log(0.1 + 0.2);
console
.log(0.07 * 100);
var num
= 0.1 + 0.2;
console
.log(num
== 0.3);
</script
>
</head
>
<body
>
</body
>
</html
>
1.2 表达式和返回值
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<meta http
-equiv
="X-UA-Compatible" content
="ie=edge">
<title
>Document
</title
>
<script
>
console
.log(1 + 1);
var num
= 1 + 1;
</script
>
</head
>
<body
>
</body
>
</html
>
1.3 递增递减运算符
递增
<script
>
var num
= 1;
num
= num
+ 1;
num
= num
+ 1;
console
.log(num
);
var age
= 10;
++age
;
console
.log(age
);
var p
= 10;
console
.log(++p
+ 10);
</script
>
<script
>
var num
= 10;
num
++;
console
.log(num
);
var age
= 10;
console
.log(age
++ + 10);
console
.log(age
);
</script
>
递增练习
<script
>
var a
= 10;
++a
;
var b
= ++a
+ 2;
console
.log(b
);
var c
= 10;
c
++;
var d
= c
++ + 2;
console
.log(d
);
var e
= 10;
var f
= e
++ + ++e
;
console
.log(f
);
</script
>
1.4 比较运算符
<script
>
console
.log(3 >= 5);
console
.log(2 <= 4);
console
.log(3 == 5);
console
.log('pink老师' == '刘德华');
console
.log(18 == 18);
console
.log(18 == '18');
console
.log(18 != 18);
console
.log(18 === 18);
console
.log(18 === '18');
</script
>
1.5 逻辑运算符
<script
>
console
.log(3 > 5 && 3 > 2);
console
.log(3 < 5 && 3 > 2);
console
.log(3 > 5 || 3 > 2);
console
.log(3 > 5 || 3 < 2);
console
.log(!true);
</script
>
1.6 短路运算(逻辑中断)
<script
>
console
.log(123 && 456);
console
.log(0 && 456);
console
.log(0 && 1 + 2 && 456 * 56789);
console
.log('' && 1 + 2 && 456 * 56789);
console
.log(123 || 456);
console
.log(123 || 456 || 456 + 123);
console
.log(0 || 456 || 456 + 123);
var num
= 0;
console
.log(123 || num
++);
console
.log(num
);
</script
>
1.7 赋值运算符
<script
>
var num
= 10;
num
+= 5;
console
.log(num
);
var age
= 2;
age
*= 3;
console
.log(age
);
</script
>
1.8 运算符优先级
<script
>
console
.log(4 >= 6 || '人' != '阿凡达' && !(12 * 2 == 144) && true)
var num
= 10;
console
.log(5 == num
/ 2 && (2 + 2 * num
).toString() === '22');
console
.log('-------------------');
var a
= 3 > 5 && 2 < 7 && 3 == 4;
console
.log(a
);
var b
= 3 <= 4 || 3 > 1 || 3 != 2;
console
.log(b
);
var c
= 2 === "2";
console
.log(c
);
var d
= !c
|| b
&& a
;
console
.log(d
);
</script
>
2. 流程控制-分支if结构
2.1 if
<script
>
if (3 < 5) {
alert('沙漠骆驼');
}
</script
>
网吧案例
<script
>
var age
= prompt('请输入您的年龄:');
if (age
>= 18) {
alert('我想带你去网吧偷耳机');
}
</script
>
2.2 if-else
<script
>
var age
= prompt('请输入您的年龄:');
if (age
>= 18) {
alert('我想带你去网吧偷耳机');
} else {
alert('滚, 回家做作业去');
}
</script
>
判断闰年案例
<script
>
var year
= prompt('请您输入年份:');
if (year
% 4 == 0 && year
% 100 != 0 || year
% 400 == 0) {
alert('您输入的年份是闰年');
} else {
alert('您输入的年份是平年');
}
</script
>
2.3 if-else-if
<script
>
if (条件表达式
1) {
} else if (条件表达式
2) {
} else if (条件表达式
3) {
} else {
}
</script
>
2.4 三元表达式
<script
>
var num
= 10;
var result
= num
> 5 ? '是的' : '不是的';
console
.log(result
);
</script
>
数字补0案例
<script
>
var time
= prompt('请您输入一个 0 ~ 59 之间的一个数字');
var result
= time
< 10 ? '0' + time
: time
;
alert(result
);
</script
>
2.5 switch
<script
>
switch (8) {
case 1:
console
.log('这是1');
break;
case 2:
console
.log('这是2');
break;
case 3:
console
.log('这是3');
break;
default:
console
.log('没有匹配结果');
}
</script
>
注意事项
<script
>
var num
= 1;
switch (num
) {
case 1:
console
.log(1);
case 2:
console
.log(2);
case 3:
console
.log(3);
break;
}
</script
>
查询水果案例
<script
>
var fruit
= prompt('请您输入查询的水果:');
switch (fruit
) {
case '苹果':
alert('苹果的价格是 3.5/斤');
break;
case '榴莲':
alert('榴莲的价格是 35/斤');
break;
default:
alert('没有此水果');
}
</script
>
转载请注明原文地址:https://ipadbbs.8miu.com/read-53876.html