1. Values
数据是0和1的海洋。
js有六种基本数据类型: Number, String, Boolean, Function, Object, Undefined.
2. Numbers
64 位。
不区分Int 和 Float。
可以用科学计数法表示: 2.998e8 , 等价于2.998 × 10
8 = 299,800,000 。
3. 数学运算
100 + 4 * 11
运算符: +, -, *, /, %
用括号改变运算符优先级: (100 + 4) * 11
4. 特殊数字
Infinity: 正的无限大
-Infinity: 负的无限大
Infinity - 1 = Infinity
Infinity 不能做精确的数字运算。
NaN:"not a number",但它还是属于Number类型。
45/0 = NaN
Infinity - Infinity = NaN
5. Strings
单引号和双引号都可以
'Patch my boat with chewing gum.'
"Monkeys wave goodbye."
escaping
字符串:"This is the first line\nAnd this is the second"
输出:This is the first line
And this is the second
字符串:"A newline character is written like \"\\n\"."
输出:A newline character is written like "\n".
字符串连接
"con" + "cat" + "e" + "nate"
结果为:"concatenate"
6. 单目运算符
typeof (获取类型,结果是字符串)
console.log(typeof 4.5);
输出:number
console.log(typeof "x");
输出:string
- (变为负数)
console.log( - (10-2) );
输出:-8
7. Boolean
true
false
8. 比较运算
console.log(3>2)
输出: true
console.log(3<2)
输出: false
console.log("about" < "bad")
输出: true
字符串的比较:根据Unicode编码的顺序。
比较运算符:>, <, >=, <=, ==, !=
console.log(NaN == NaN)
输出:false
这是一个特殊案例,自己不等于自己。
9. 逻辑运算
9.1. and: &&
console.log(true && false)
输出: false
console.log(true && true)
输出: true
9.2. or: ||
console.log(true && false)
输出: true
console.log(false && false)
输出: false
9.3. not: !
console.log(!true)
输出: false
9.4. 三目运算符:
console.log(true ? 1 : 2)
输出: 1
console.log(false ? 1 : 2)
输出: 2
10. Undefined 类型
undefined
null
11. 自动类型转换
8 * null // 0
"5" - 1 // 4
"5" + 1 // "51"
"five" * 2 // NaN
false == 0 // true
null == undefined // true, null 和 undefined 被认为是相同的值,绝大部分情况下可以互相替换。
null == 0 // false, 只有undefined才和null相等,其它任何值都不行。
"1" == 1 // true
精确匹配运算符:===, !== (防止自动类型转换)
"1" === 1 // false
12. 逻辑运算的短路
a > b || (a + c) > b
如果|| 左侧的运算结果为true,则,右侧的运算过程会被省略。