JavaScript 是一种弱类型和动态类型语言。这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定。
JS 数据类型有哪些?
最新的 ECMAScript 标准定义了 8 种数据类型,可以分为两大类:
-
7 种原始类型
-
Boolean布尔类型,表示一个逻辑实体,可以有两个值true和false。 -
NullNull 类型,只有一个值null。 -
Undefined一个没有被赋值的变量会有个默认值undefined。 -
Number数字类型。 -
BigInt数值类型,ES2020 新增,可以表示任意大的整数。 -
String字符串类型,用于表示文本数据。 -
Symbol符号类型,ES2015 新增,表示独一无二的值。
-
-
和
Obeject对象类型(以下同属于Obeject数据类型,使用typeof可得)-
Obeject对象,可以被看作是一组属性的集合。 -
Array数组,用于构造数组的全局对象,数组是类似于列表的高阶对象。 -
Date日期
-
如何判断数据类型?
1. 使用 typeof 判断数据类型
判断 JS 数据类型最常用的是 typeof,使用它会返回一个字符串,需要注意的是,不管对象是不是数组,使用 typeof 都会返回一个 object。
console.log(typeof false); // expected output: boolean
console.log(typeof null); // ? expected output: object
console.log(typeof undefined); // expected output: undefined
console.log(typeof 1); // expected output: number
console.log(typeof 10n); // expected output: bigint
console.log(typeof "hello world"); // expected output: string
console.log(typeof Symbol()); // expected output: symbol
console.log(typeof new Object()); // expected output: object
console.log(typeof new Array()); // expected output: object
console.log(typeof new Date()); // expected output: object
console.log(typeof function(){}); // expected output: function
2. 使用 instanceof 判断数据类型
instanceof 运算符可以用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。使用它会返回一个布尔值,由此判断出数据类型。
let arr = [1, 2, 3];
let obj = { a: 1 };
let fn = function () {};
console.log(arr instanceof Array); // expected output: true
console.log(obj instanceof Object); // expected output: true
console.log(fn instanceof Function); // expected output: true
3. 使用 Object.prototype.toString.call() 方法
console.log(Object.prototype.toString.call([])); // expected output: [object Array]
console.log(Object.prototype.toString.call({})); // expected output: [object Object]
console.log(Object.prototype.toString.call(1)); // expected output: [object Number]
console.log(Object.prototype.toString.call(false)); // expected output: [object Boolean]
console.log(Object.prototype.toString.call(Symbol())); // expected output: [object Symbol]
console.log(Object.prototype.toString.call('str')); // expected output: [object String]
console.log(Object.prototype.toString.call(null)); // expected output: [object Null]
console.log(Object.prototype.toString.call(undefined)); // expected output: [object Undefined]
console.log(Object.prototype.toString.call(function(){})); // expected output: [object Function]
4. 使用 typeof 结合 Object.prototype.toString.call() 封装一个方法
// 方法
function getType(data) {
let type = typeof data;
if (type !== "object") {
return type;
}
return Object.prototype.toString
.call(data)
.replace(/^\[object (\S+)\]$/, "$1")
.toLowerCase();
}
// 测试
getType([1, 2, 3]); // expected output: array
getType({ a: 1 }); // expected output: object
getType(function () {}); // expected output: function
如何进行类型转换?
将数字转换为字符串
// 显式转换
String(123)) // expected output: "123"
(123).toString() // expected output: "123"
(123).toFixed() // expected output: "123"
// 隐式转换
let a = 123;
let b = a + "";
console.log(b) // expected output: "123"
将布尔值转换为字符串
// 显式转换
String(false) // expected output: "false"
String(true) // expected output: "true"
false.toString() // expected output: "false"
true.toString() // expected output: "true"
将字符串转换为数字
// 显式转换
Number("3.14") // expected output: 3.14
Number(" ") // expected output: 0
Number("") // expected output: 0
Number("99 88") // expected output: NaN
parseFloat("1.2") // expected output: 1.2
parseInt("1") // expected output: 1
// 隐式转换
let a = "1";
let b = +a;
console.log(b); // expected output: 1
将布尔值转换为数字
Number(false) // expected output: 0
Number(true) // expected output: 1









网友评论