-
typeof 语法
用于检测变量的数据类型。typeof(1); typeof 1;
-
typeof的返回值
string, boolean, number, bigint, symbol, function, object
-
typeof 示例
typeof '1' // string typeof toString(1) // string typeof 'true' // string typeof '[1, 2, 4]' // string typeof('1' + 1) // string typeof '1' + 1 // string1,请注意,在对变量做运算时,不可使用+。
typeof true // boolean typeof false // boolean
typeof null; // object // null是一个只有一个值的特殊类型。表示一个空对象引用。 // 在清空变量时,重置成null是一个好办法,可以有效语法奇怪的问题出现。 let a = null; // 但在验证null时,请使用 ===,因为==无法区分null和undefined。
typeof undefined // undefined
// undefined 和 null 的区别 typeof undefined // undefined typeof null // object null == undefined // true null === undefined // false
typeof 1; // number typeof NaN; // number typeof 42n; // bigint
typeof Symbol(); // symbol typeof Symbol('a'); // symbol
function t() {}; typeof t; // function typeof class a {}; // function typeof String; // function typeof Array; // function typeof new Function; // function typeof RegExp; // function
typeof {} // 'object' typeof [] // 'object' typeof null // 'object' typeof Math // 'object' typeof new Number(1) // 'object'
【typeof的用法】
最新推荐文章于 2024-02-05 21:59:55 发布