-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_logic.html
More file actions
86 lines (71 loc) · 2.55 KB
/
js_logic.html
File metadata and controls
86 lines (71 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!--
* @由于个人水平有限, 难免有些错误, 还请指点:
* @Author: cpu_code
* @Date: 2020-10-17 23:05:03
* @LastEditTime: 2020-10-17 23:32:24
* @FilePath: \web\javascript\js_logic\js_logic.html
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
* @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
* @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>逻辑运算符</title>
<script>
/*
* 逻辑运算符:
* &&: 与(短路)
* ||: 或 (短路)
* !:非
* * 其他类型转boolean:
* 1. number:0或NaN为假,其他为真
* 2. string:除了空字符串(""),其他都是true
* 3. null&undefined:都是false
* 4. 对象:所有对象都为true
*/
var flag = true;
document.write("flag = " + flag + "<br>");
document.write("!flag = " + !flag + "<br>");
document.write("<hr>");
//数字
var num = 3;
var num2 = 33;
var num3 = NaN;
document.write("!!num = " + !!num + "<br>");
document.write("!!num2 = " + !!num2 + "<br>");
document.write("!!num3 = " + !!num3 + "<br>");
document.write("<hr>");
//string
var str1 = "abc";
var str2 = "";
document.write("!!str1 = " + !!str1 + "<br>");
document.write("!!str2 = " + !!str2 + "<br>");
document.write("<hr>");
// null & undefined
var obj = null;
var obj2;
document.write("!!obj = " + !!obj + "<br>");
document.write("!!obj2 = " + !!obj2 + "<br>");
document.write("<hr>");
// null & undefined
var date = new Date();
document.write("!!date = " + !!date + "<br>");
document.write("<hr>");
obj = "111";
//防止空指针异常
if(obj != null && obj.length > 0) {
alert(111);
}
//js中可以这样定义,简化书写
////防止空指针异常
if(obj){
alert(222);
}
</script>
</head>
<body>
</body>
</html>