typeof

Examples:

// Numbers

typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; 
// Despite being "Not-A-Number"

typeof Number(1) === 'number'; 
// but never use this form!



// Strings

typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; 
// typeof always returns a string

typeof String("abc") === 'string'; 
// but never use this form!



// Booleans

typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; 
// but never use this form!



// Symbols

typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'


// Undefined

typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined'; 


// Objects

typeof {a:1} === 'object';

// use Array.isArray or Object.prototype.toString.call

// to differentiate regular objects from arrays

typeof [1, 2, 4] === 'object';

typeof new Date() === 'object';


// The following is confusing. Don't use!

typeof new Boolean(true) === 'object'; 
typeof new Number(1) === 'object'; 
typeof new String("abc") === 'object';


// Functions

typeof function(){} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';
// This stands since the beginning of JavaScript

typeof null === 'object';
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容