js數(shù)據(jù)類型以及其判斷方法實(shí)例
js的數(shù)據(jù)類型
基本數(shù)據(jù)類型:number , string , boolean , undefined , null , Symbol,
引用數(shù)據(jù)類型:object
NaN 屬于 number;
Function, Array, Date 都屬于 object;
基本數(shù)據(jù)類型除 null 都可以通過 typeof 判斷,引用數(shù)據(jù)類型除 Function 外都返回 Ojbect
let a = 1,
b = '2',
c = true,
d = undefined,
e = null,
f = Symbol('f'),
g = function () {},
h = [],
i = new Date()
console.log(typeof a)
console.log(typeof b)
console.log(typeof c)
console.log(typeof d)
console.log(typeof e)
console.log(typeof f)
console.log(typeof g)
console.log(typeof h)
console.log(typeof i)
查看輸出結(jié)果

可以看到 null 的 typeof 是 object , 這屬于歷史bug ,有興趣可以參考《The history of “typeof null” 》
可通過以下方法判斷 null
function checkNull(num) {
return num === null
}
object 的詳細(xì)類型可通過 Object.prototype.toString.call() 判斷
function checkObject(obj) {
return Object.prototype.toString.call(obj)
}
console.log(checkObject(g))
console.log(checkObject(h))
console.log(checkObject(i))
可看到輸出結(jié)果

也可通過構(gòu)造函數(shù) constructor() 判斷
console.log(g.constructor === Function) console.log(h.constructor === Array) console.log(i.constructor === Date)
可看到輸出結(jié)果

總結(jié)
到此這篇關(guān)于js數(shù)據(jù)類型以及其判斷方法的文章就介紹到這了,更多相關(guān)js數(shù)據(jù)類型及判斷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript判斷一個(gè)字符串是否包含指定子字符串的方法
這篇文章主要介紹了JavaScript判斷一個(gè)字符串是否包含指定子字符串的方法,實(shí)例分析了javascript字符串操作的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
修改js confirm alert 提示框文字的簡單實(shí)例
下面小編就為大家?guī)硪黄薷膉s confirm alert 提示框文字的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
原生JS實(shí)現(xiàn)頂部導(dǎo)航欄顯示按鈕+搜索框功能
這篇文章主要介紹了原生js實(shí)現(xiàn)頂部導(dǎo)航欄顯示按鈕+搜索框功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
JS組件中bootstrap multiselect兩大組件較量
這篇文章主要介紹了JS組件中bootstrap multiselect兩大組件,兩者之間的較量,優(yōu)缺點(diǎn)比較,感興趣的小伙伴們可以參考一下2016-01-01

