js中typeof 方法使用

2021-08-10 10:13:31 字數 691 閱讀 3975

typeof 運算子把型別資訊當作字串返回。typeof 返回值有六種可能: "number," "string," "boolean," "object," "function," 和 "undefined." 

typeof的運算數未定義,返回的就是 "undefined".
運算數為數字 typeof(x) = "number" 

字串 typeof(x) = "string"

布林值 typeof(x) = "boolean"

物件,陣列和null typeof(x) = "object"

函式 typeof(x) = "function"

typeof 運算子返回乙個用來表示表示式的資料型別的字串。

可能的字串有:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。

如: alert(typeof (123));//typeof(123)返回"number"

alert(typeof ("123"));//typeof("123")返回"string"

js 中 typeof 的使用

js中的變數是鬆散型別 即弱型別 的,可以用來儲存任何型別的資料。typeof 可以用來檢測給定變數的資料型別,可能的返回值 undefined 這個值未定義 boolean 這個值是布林值 string 這個值是字串 number 這個值是數值 object 這個值是物件或null functio...

js中typeof的小結

首先,js中預設的原始型別有 js中預設的內建物件型別有 用原始型別定義方式 var num 1 alert typeof num 同alert typeof num 輸出number 如果用物件的方式 var num new number 1 同var num number 1 alert typ...

js中typeof的用法

typeof是乙個運算子,有2種使用方式 typeof 表示式 和typeof 變數名,第一種是對表示式做運算,第二種是對變數做運算。typeof運算子的返回型別為字串,值包括如下幾種 1.undefined 未定義的變數或值 2.boolean 布林型別的變數或值 3.string 字串型別的變數...