Javascript数据类型
number
string
boolean{
下列运算符会返回布尔值:
以下六个值会被自动转换为false,其余都视为true
空数组([]
)和空对象({}
)对应的布尔值,都是true
。
if ([]) {
console.log('true');
}
// true
if ({
}) {
console.log('true');
}
// true
}
undefined{
表示“未定义”或不存在,即由于目前没有定义,所以此处暂时没有任何值。转为数值时为NaN
}
null{
表示空值,即此处的值为空。转为数值时为0
}
object{
各种值组成的集合。
{
}
}
primitive type{
原始类型,包括number,string,boolean
}
ES6新增了Symbol类型
typeof{
运算符可以返回一个值的数据类型。
typeof 123 // "number"
typeof '123' // "string"
typeof false // "boolean"
function f() {
}
typeof f
// "function"
typeof undefined
// "undefined"
v
// ReferenceError: v is not defined
typeof v
// "undefined"
if (typeof v === "undefined") {
// ...
}
typeof window // "object"
typeof {
} // "object"
typeof [] // "object"
}
JavaScript 内部,所有数字都是以64位浮点数形式储存,即使整数也是如此。
0.1 + 0.2 === 0.3
// false
0.3 / 0.1
// 2.9999999999999996
(0.3 - 0.2) === (0.2