11.typeof操作符

一道题目

typeof typeof typeof null; //'string'

typeof操作符

typeof 操作符返回一个字符串,表示未经计算的操作数的类型。

语法:

typeof operand
typeof(operand)

1、数值

console.log(typeof 42);        //'number'
console.log(typeof 3.14);      //'number'
console.log(typeof(42));       //'number'
console.log(typeof Math.LN2);  //'number'
console.log(typeof Infinity);  //'number'
console.log(typeof NaN);       //'number'
console.log(typeof Number(1)); //'number'
console.log(typeof 42n);       //'bigint'

2、字符串

console.log(typeof '');         //'string'
console.log(typeof 'hello');    //'string'
console.log(typeof `hello`);    //'string'
console.log(typeof '1');        //'string'
console.log(typeof (typeof 1)); //'string'
console.log(typeof String(1));  //'string'

3、布尔值

console.log(typeof true);       //'boolean'
console.log(typeof false);      //'boolean'

//Boolean() 会基于参数是真值还是虚值进行转换
console.log(typeof Boolean(1)); //'boolean' 

//两次调用 ! (逻辑非) 操作符相当于 Boolean()
console.log(typeof !!(1));      //'boolean'

4、Symbols

console.log(typeof Symbol());        //'symbol'
console.log(typeof Symbol('foo'));   //'symbol'
console.log(typeof Symbol.iterator); //'symbol'

5、Undefined

console.log(typeof undefined);  //'undefined'

//declaredButUndefinedVariable
var firstname;
console.log(typeof firstname);  //'undefined'

//undeclaredVariable
console.log(typeof lastname);   //'undefined'

6、对象

console.log(typeof {a:1});      //'object'
console.log(typeof [1,2,3]);    //'object'
console.log(typeof new Date()); //'object'
console.log(typeof /\d/);       //'object'

console.log(typeof new Boolean(true));  //'object'
console.log(typeof new Number(1));      //'object'
console.log(typeof new String('1'));    //'object'

7、函数

console.log(typeof function(){}); //'function'
console.log(typeof class C {});   //'function'
console.log(typeof Math.sin);     //'function'

8、typeof null

typeof null; //'object'

9、new操作符

除 Function 外的所有构造函数的类型都是 ‘object’

console.log(typeof new String('1')); //'object'
console.log(typeof new Number(100)); //'object'
console.log(typeof new Function());  //'function'

10、括号

// 括号有无将决定表达式的类型。
var iData = 99;
typeof iData + ' Wisen'; // 'number Wisen'
typeof (iData + ' Wisen'); // 'string'

11、正则

typeof /s/; //'object'

12、错误

console.log(typeof undeclaredVariable === 'undefined'); //true

console.log(typeof newLetVariable); // Uncaught ReferenceError: newLetVariable is not defined
console.log(typeof newConstVariable); // Uncaught ReferenceError: newConstVariable is not defined
console.log(typeof newClass); // Uncaught ReferenceError: newClass is not defined

let newLetVariable;
const newConstVariable = 'hello';
class newClass{};

13、例外

console.log(typeof document.all); //'undefined'
console.log(typeof document.all === 'undefined'); //true
console.log(typeof(document.all === 'undefined'));//'boolean'

14、宿主对象

chrome 88.0.4324.96(正式版本) (x86_64)

typeof alert; //'function'

More

typeof
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof