【typeof的用法】

  1. typeof 语法
    用于检测变量的数据类型。

    typeof(1);
    typeof 1;
    
  2. typeof的返回值

    string, boolean, number, bigint, symbol, function, object
    
  3. 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'
    
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值