基本数据类型有7种:number、string、boolean、null、undefined、symbol、bigint
//typeof 关键字,可以判断数据类型
number 数字
不带引号的数字,包括负数、小数等都是数字类型
const age = 18;
console.log(typeof age); // number
string 字符串
两边有单引号或者双引号的字符
const str = 'hello';
console.log(typeof str); // string
boolean 布尔型
布尔值只有两个,true、false
true表示是,false表示否,通常用来做判断
const flag = true;
console.log(typeof flag); // boolean
null 空
// 空值会被typeof判断为object
let obj = null;
console.log(typeof obj); // object
undefined 未赋值
字面意思,例如一个变量未赋值,那么他的数据类型就是undefined
// 未赋值的变量默认值是undefined,类型也是undefined
let num;
console.log(typeof num); // undefined
symbol
symbol 表示一个独一无二的值
let num = Symbol(); // 使用Symbol()方法定义一个symbol
console.log(typeof num); // symbol
bigint
表示大于2^53 - 1
整数,可以用来表示任意大的整数
const num = 10n; // 在整数后面加n来定义一个bigint
console.log(typeof num); // bigint