1.Object.prototype.toString.call()
可以判斷任何類型,包括對null,undefined的判斷
2.instanceof
instanceof 的內部機制是通過判斷對象的原型鏈中是不是能找到類型的 prototype,能在實例上找到的類型
[1,3] instanceof Object //true
[1,3] instanceof Array //true
'123' instanceof String //false
let str= new String('123')
str instanceof String //true
3.typeof
和instanceof相比,type返回的數據類型,但是instanceof返回布爾類型true/false。typeof有一個缺陷就是判斷null的時候,返回爲object.
4.Array.isArray()
判斷是否爲數組
個人推薦使用第一個Object.prototype.toString.call(),都能返回相應的數據類型,無論是null還是undefined。