判断数据类型的方法

1、typeof 判断数据类型  

var add=function(){
console.log("hello world!")
}

typeof  "world'           String
typeof   123              Number
typeof {"age" :"23"}      Object
typeof  [2,4,5,6]         Object
typeof  null              Object
typeof  false             Boolean
typeof  underfind         Underfind
typeof  add               Function

由此可见,typeof还是有局限性的,对与数组和对象是无法区分出来的,给大家推荐一个终极方法,请看如下:

2、Object.prototype.toString

var type=Object.prototype.toString

type.call("hello")             [object  String]
type.call(1233)                [object   Number]
typeof.call(false)             [object   Boolean]
typeof.call([1,3])             [object   Array]
typeof.call(function(){})      [object   Function]
typeof.call({})                [object   Object]
typeof.call(underfind)         [object   Underfind]
typeof.call(null)              [object   Null]

对每一天的总结,都是一种进步!!

原文地址:https://www.cnblogs.com/missya/p/10639745.html