javascript 教程-数据类型 检查

2016-12-07  本文已影响0人  sponing
数据类型

字符串(String)、数字(Number)、布尔(Boolean)、数组(Array)、对象(Object)、空(Null)、未定义(Undefined)。

检查数据类型的方法

1,typeof
2,instanceof

typeof

写法:typeof 变量/数据  
typeof "John"                // 返回 string 
typeof 3.14                  // 返回 number
typeof false                 // 返回 boolean
typeof [1,2,3,4]             // 返回 object
typeof {name:'John', age:34} // 返回 object

typeof undefined             // undefined
typeof null                  // object 特殊的对象
null === undefined           // false ===是数据类型的比较
null == undefined           //true ==单单比较 没有数据类型

对于数组 会返回object 因为数组是特殊的对象 这个检查数组就要用instanceof

instanceof

instanceof只会返回return true/false 真/假
写法:变量/数据  instanceof 数据类型
var a = [1,2,3,4];
var b =  a instanceof Array;
var c = a instaceof String ;
console.log(b); //true
console.log(c); //false
上一篇 下一篇

猜你喜欢

热点阅读