特點(diǎn)
typeof運(yùn)算符是一個(gè)一元運(yùn)算符部凑,不是函數(shù)
用法
typeof 運(yùn)算數(shù)
typeof(運(yùn)算數(shù))
返回值
返回值是字符串聊品,全部為小寫(xiě)字母
結(jié)果
'string'
'number'
'boolean'
'function'
'undefined'
'object'
原始類(lèi)型
字符串涕癣、數(shù)字冷尉、布爾值分別返回'string'
歪赢、'number'
慧瘤、'boolean'
var str = '123';
typeof str //'string'
var num1 = 123;
var num2 = NaN;
typeof num1 //'number'
typeof num2 //'number'
typeof true //'boolean'
typeof false //'boolean'
函數(shù)
函數(shù)返回'function'
function fn(){};
var a = function(){};
typeof fn //'function'
typeof a //'function'
undefined
undefined返回'undefined'
未聲明變量返回'undefined'
變量未聲明在使用的時(shí)候報(bào)錯(cuò),但是使用typeof運(yùn)算符則是返回undefined
typeof undefined //'undefined'
typeof a //'undefined'
a //報(bào)錯(cuò) a is not defined
其他
除上述情況外本橙,都返回'object'
typeof window //'object'
typeof document //'object'
typeof [] //'object'
typeof {} //'object'
typeof null //'object'