六种数据类型
原始类型 | 对象 |
---|
object | Function |
string | Array |
null | Date |
number | |
boolean | |
undefined | |
隐式转换
文本与数值转换
- “文本数值”-数值 eg:
"31"-0```` 结果为31,减号在这里被理解为减法运算,位置可以对调
"31"+0```` 结果为310,加号在这里被理解为文本拼接,位置可以对调
==与===的区别
- 在==中
- 数字1和0会变转为true和false, 1 == true 和 0 == false 成立
- 文本数字会转为数字进行比较, “10” == 10 是成立的
- null == undefined 是成立的
- 在===中
类型检测
Typeof函数检测
typeof函数 | 返回值类型 |
---|
typeof 100 | “number” |
typeof false | “boolean” |
typeof function | “function” |
typeof(undefined) | “undefined” |
typeof new Object() | “object” |
typeof [1,2] | “obeject” |
typeof NaN | “number” |
typeof null | “obeject” |
Object.prototype.toString:原生原型扩展函数,用来更精确的区分数据类型
var gettype=Object.prototype.toString **先声明一个对象**
输出 | 返回值类型 |
---|
gettype.call(‘AaBbCc’) | [object String] |
gettype.call(123456) | [object Number] |
gettype.call(false) | [object Boolean] |
gettype.call(undef | [object Undefined] |
gettype.call(null) | [object Null] |
gettype.call({}) | [object Object] |
gettype.call([]) | [object Array] |
gettype.call(function(){ | [object Function] |
判断还有用constructor,instanceof
可以看看这里,这里有更为精准的描述
typeof 和 instanceOf的区别_segmentfault
Javascript typeof 和instanceof by hongweigg