js判斷資料型別的幾種方式

2021-10-01 03:53:40 字數 625 閱讀 9672

資料型別分為 基礎資料型別和引用(物件)資料型別

基礎資料型別: number string boolean undefined null

常見的引用資料型別: funciton object array

第一種,當資料型別是undefined null 時,可以用 ===來進行判斷

undefined === undefined

null === null

第二種 最常見的typeof 方法,返回乙個字串的

typeof的返回值包括

string object function number boolean undefined

typeof(想要測試的資料) 或者 typeof 想要測試的資料

第三種 通過instanceof 方法,判斷引用值的型別

instanceof只能用來判斷資料是否是object array function這三種,返回的是乙個布林型別的值

想要測試的資料 instanceof object

想要測試的資料 instanceof array

想要測試的資料 instanceof function

判斷資料型別的幾種方式

通常判斷js中的資料型別有常用的幾種方式 判斷基本資料型別 typeof 判斷復合資料型別 instanceof 判斷全部的資料型別 object.prototype.tostring.call var sum 23 var str 王八蛋 var obj var arr 1,2,3,54,56,7...

js判斷資料型別幾種方法

js資料型別的判斷主要有四種方法 typeof instanceof constructor object.prototype.tostring.call 資料型別的包括 number boolean string symbol object array undefined null functio...

js判斷資料型別的幾種方法

判斷js中的資料型別有一下幾種方法 typeof instanceof constructor prototype type jquery.type 接下來主要比較一下這幾種方法的異同。先舉幾個例子 var a iamstring.var b 222 var c 1,2,3 var d new da...