001-JS数据类型及判断

1、数据类型

JavaScript 包含 6 中基本数据类型 和 引用类型:
6种基本类型:字符串(String)、数字(Number)、布尔(Boolean)、空(Null)、未定义(Undefined)、Symbol;
引用数据类型:对象(Object)、数组(Array);

2、数据类型判断方法

💡 Tips:null 通过等于自身判断,其他基本类型通过 typeof 判断,引用类型通过 Object.prototype.toString 判断

function judgeType(data) {
  let type = typeof data;
  if (data === null) {
    return 'null';
  }
  if (type === 'object') {
    let typeStr = Object.prototype.toString.call(data).split(' ')[1];
    type = typeStr.slice(0, -1);
  }
  return type;
}

judgeType('123'); // string
judgeType(123); // number
judgeType(true); // boolean
judgeType(null);  // null
judgeType();  // undefined
judgeType(Symbol('name'));  // symbol
judgeType({});  // Object
judgeType([]);  // Array

3、JS的假值

💡 Tips:假值就是布尔转换后为false的值
JavaScript 的假值都有:0、-0、0.0、null、“”、false、undefined、NaN
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值