js类型检测

typeof和instanceof

var name = 'sam';

console.log(typeof name);

typeof 操作符会告诉你对象的类型是 string.number,function,boolean,undefined不过,对于其他变量,typeof只会告诉你object。

具体需要确定什么类型的,这时 instanceof 就派上用场了:

console.log(anArray instanceof Array)  //=> true

console.log(anObject instanceof Array)  //=> false

原文地址:https://www.cnblogs.com/chenjef/p/4918777.html