2013年6月29日 星期六

JavaScript typeof 回傳值

範例測試:
特別注意 typeof  對 null、[]、undefined、NaN、Infinity 的回傳值
/***/
typeof 123; // "number"
typeof '123'; // "string"
typeof true; // "boolean"

/***/
typeof Number(1); // "number"
typeof String("abc"); // "string"
typeof Boolean(true); // "boolean"

/***/
typeof new Date(); // "object"
typeof new Boolean(true); // "object"
typeof new Number(1); // "object"
typeof new String("abc"); // "object"

/***/
typeof Math; // "object"
typeof Math.PI; // "number"
typeof Math.sin; // "function"

/***/
typeof abc; // "undefined" (未定義的變數)

/***/
typeof null; // "object"
typeof []; // "object"
typeof undefined; // "undefined"
typeof NaN; // "number"
typeof Infinity; // "number"
typeof {}; // "object"
typeof function(){}; // "function"
參考
https://developer.mozilla.org/zh-TW/docs/JavaScript/Reference/Operators/typeof

沒有留言:

張貼留言