JavaScript中的数据类型
本文解释了JavaScript中的数据类型。
YouTube Video
JavaScript中的数据类型
JavaScript具有用于分类值的数据类型,可大致分为基本类型和对象类型。数据类型决定了变量中存储的数据种类,并影响其如何被操作。
基本类型
基本类型是表示单一值的简单数据类型。JavaScript具有以下7种基本类型:。
number
1let num = 42;
2let pi = 3.14;
3
4console.log("Value of num:", num);
5console.log("Value of pi:", pi);number是一种用于处理数值的类型,包括整数和浮点数(小数)。处理超过某些限制的值将导致Infinity或NaN(非数字)。
1// Decimal calculations
2console.log(0.1 + 0.2 === 0.3); // false
3console.log(0.1 + 0.2); // 0.30000000000000004
4
5// Calculations with large numbers
6console.log(9007199254740991 + 1); // correct value
7console.log(9007199254740991 + 2); // incorrect value
- 在 JavaScript 中,数字以浮点数的形式表示。因此,某些小数(尤其是那些无法以二进制精确表示的小数)可能无法准确表达。
- 当需要精确计算时,例如用于货币价值的计算,必须避免此类舍入误差。例如,可以使用
BigInt或使用toFixed()调整小数位数。
string
1let greeting = "Hello, world!";
2let char = 'A';
3const message = `"${greeting}" in JavaScript`;
4
5console.log("Value of greeting:", greeting);
6console.log("Value of char:", char);
7console.log("Value of message:", message);string是一种用于处理文本的数据类型,表示一串字符。它通过将文本用单引号(')或双引号(")括起来来表示。从ES6开始,模板字符串允许使用反引号()轻松嵌入变量。要嵌入变量,请使用${}`语法。
boolean
1let isAvailable = true;
2let hasError = false;
3
4console.log("Value of isAvailable:", isAvailable);
5console.log("Value of hasError:", hasError);boolean表示逻辑值,只能有两个值:true或false。它用作条件表达式的结果或标志。
null
1let result = null;
2
3console.log("Value of result:", result);null是一种明确表示“没有值”的数据类型。null由开发者设置以表示有意的空值。
undefined
1let score;
2let subject = undefined;
3
4console.log("Value of score:", score); // undefined
5console.log("Value of subject:", subject);undefined是一种表示“未定义”的数据类型。当一个变量被声明但没有赋值时,会自动分配undefined。
bigint
1let bigIntValue = 9007199254740991n;
2let anotherBigInt = 123456789012345678901234567890n;
3
4console.log("Value of bigIntValue:", bigIntValue);
5console.log("Value of anotherBigInt:", anotherBigInt);bigint是一种用于处理大整数的数据类型。bigint能够准确表示无法由number类型表示的大整数。bigint字面量通过在数字末尾添加n来表示。bigint是一种处理任意大小整数的类型,没有范围限制。然而,bigint和number不能直接一起运算,因此需要注意。
Symbol
1let sym1 = Symbol('id');
2let sym2 = Symbol('id');
3
4console.log("sym1 === sym2:", sym1 === sym2); // false
Symbol是一种用于创建唯一标识符的数据类型。它通过Symbol()创建,可以用作对象的属性。与其他基本类型不同,Symbol具有唯一的值,即使内容相同,也会被视为不同的Symbol。
对象类型
对象类型是可以存储多个值的数据结构。对象是键值对的集合,可以具有属性和方法。
Object
1let person = {
2 name: "John",
3 age: 30,
4 isEmployee: true
5};
6console.log(person.name); // John
Object是一个包含 属性(键值对)的集合,可用于存储多种类型的数据。对象以大括号{}表示,每个属性的键和值之间通过:连接。
Array
1let numbers = [10, 20, 30];
2console.log(numbers[0]); // 10
3console.log(numbers[1]); // 20
4console.log(numbers[2]); // 30
5console.log(numbers[3]); // undefined
Array是一个按有序方式保存多个值的列表。数组以方括号[]表示,值之间用逗号,分隔。每个元素通过从 0 开始的索引访问。
Function
1function greet(name) {
2 return "Hello, " + name;
3}
4console.log(greet("Alice")); // Hello, Alice
- 函数 是一种对象并且是可重用的代码块。可以使用
function关键字定义函数。
其他对象类型
- JavaScript 还具有内置的对象类型,例如
Date、RegExp、Map和Set。它们根据特定的使用情况来处理数据。
类型转换
在 JavaScript 中经常执行隐式和显式类型转换。
隐式类型转换
- JavaScript 在对不同类型进行操作时可能会自动转换类型。
1let result = 5 + "5"; // "55" (the number is converted to a string)
2
3console.log(result);- 在此示例中,数字
5被隐式转换为字符串,结果是字符串"55"。
显式类型转换
- 开发人员也可以显式地执行类型转换。
1let num = "123";
2let convertedNum = Number(num); // Converts "123" to the number 123
3
4console.log(typeof num);
5console.log(typeof convertedNum);- 在此示例中,使用
Number()函数显式地将字符串"123"转换为数字123。
类型检查
在 JavaScript 中,可以使用 typeof 操作符检查变量的数据类型。
1// Output: The type of 42 is: number
2console.log('The type of 42 is:', typeof 42);
3
4// Output: The type of 42 is: bigint
5console.log('The type of 42n is:', typeof 42n);
6
7// Output: The type of "hello" is: string
8console.log('The type of "hello" is:', typeof "hello");
9
10// Output: The type of true is: boolean
11console.log('The type of true is:', typeof true);
12
13// Output: The type of undefined is: undefined
14console.log('The type of undefined is:', typeof undefined);
15
16// Output: The type of null is: object
17console.log('The type of null is:', typeof null);
18
19// Output: The type of Symbol() is: symbol
20console.log('The type of Symbol() is:', typeof Symbol());
21
22// Output: The type of {} is: object
23console.log('The type of {} is:', typeof {});
24
25// Output: The type of function () {} is: function
26console.log('The type of function () {} is:', typeof function () {});结论
- 基本类型 包括
number、string、boolean、null、undefined和symbol。 - 对象类型 包括
Object、Array、Function等。 - 由于既会发生自动类型转换,也会发生显式类型转换,因此理解正确的数据类型使用方式非常重要。
您可以在我们的YouTube频道上使用Visual Studio Code跟随上述文章进行学习。 请也查看我们的YouTube频道。