typeof 是用來判斷值型別的一元運算子,會回傳字串,例如 number、string、boolean、object、function 與 undefined。
數字、NaN、Infinity 都會回傳 number。字串回傳 string,布林值回傳 boolean。物件、陣列與 null 都會回傳 object,函式則回傳 function。
若要判斷變數是否存在,直接比較 typeof 的結果即可,例如檢查是否等於 undefined。若要判斷陣列,建議使用 Array.isArray,因為 typeof 無法區分陣列與一般物件。