JavaScript 数据类型是指我们正在存储或使用的值的类型。编程语言最基本的特征之一是它支持的数据类型集。这些是可以用编程语言表示和操作的值类型。
JavaScript 数据类型可分为基元和非基元 (对象) 。JavaScript(ES6 及更高版本)允许您使用七种原始数据类型 -
- Strings ,例如 “This text string” 等
- Numbers, 例如。123、120.50 等
- Boolean ,例如 true 或 false。
- null
- undefined
- BigInt
- Symbol
BigInt 和 Symbol 是在 ES6 中引入的。在 ES5 中,只有 5 种原始数据类型。
除了这些原始数据类型之外,JavaScript 还支持一种称为 object 的复合数据类型。我们将在单独的章节中详细介绍对象。
Object 数据类型包含 3 种子数据类型 -
- Object
- Array
- Date
为什么数据类型很重要?
在任何编程语言中,数据类型对于操作操作都很重要。
例如,下面的代码生成 “1010” 输出。
let sum = "10" + 10;
在这里,JavaScript 引擎将第二个操作数转换为字符串,并使用 '+' 运算符将其组合起来,而不是添加它们。
因此,您需要确保操作数的类型正确。
现在,让我们通过示例了解每种数据类型。
JavaScript 字符串(Strings)
在 JavaScript 中,字符串是一系列字符,可以使用下面给出的 3 种不同的方式创建 -
- 使用单引号
- 使用双引号
- 使用反引号
例
在下面的示例中,我们使用单引号、双引号和反引号创建了字符串。在输出中,它为所有 3 个字符串打印相同的结果。
<html>
<head>
<title> JavaScript string </title>
</head>
<body>
<script>
let str1 = "Hello World!"; // 使用双引号
let str2 = 'Hello World!'; // 使用单引号
let str3 = `Hello World!`; // 使用反引号
document.write(str1 + "<br>");
document.write(str2 + "<br>");
document.write(str3 + "<br>");
</script>
</body>
</html>
JavaScript 数字(Numbers)
- JavaScript 数字始终存储为浮点值(十进制数)。
- JavaScript 不区分整数值和浮点值。
- JavaScript 使用 IEEE 754 标准定义的 64 位浮点格式表示数字。
在下面的示例中,我们演示了带小数点和不带小数点的 JavaScript 数字。
<html>
<head>
<title> JavaScript number </title>
</head>
<body>
<script>
let num1 = 10; // Integer
let num2 = 10.22; // Floating point number
document.write("The value of num1 is " + num1 + "<br/>");
document.write("The value of num2 is " + num2);
</script>
</body>
</html>
示例(数字的指数表示法)
JavaScript 还支持数字的指数概念。我们在下面的示例代码中对此进行了解释 -
<html>
<head>
<title> JavaScript number Exponential notation </title>
</head>
<body>
<script>
let num1 = 98e4; // 980000
let num2 = 98e-4; // 0.0098
document.write("The value of num1 is: " + num1 + "<br/>");
document.write("The value of num2 is: " + num2);
</script>
</body>
</html>
JavaScript 布尔值(Boolean)
在 JavaScript 中,Boolean 数据类型只有两个值:true 或 false。
<html>
<head>
<title> JavaScript Boolean </title>
</head>
<body>
<script>
let bool1 = true;
let bool2 = false;
document.write("The value of the bool1 is " + bool1 + "<br/>");
document.write("The value of the bool2 is " + bool2 + "<br/>");
</script>
</body>
</html>
JavaScript 未定义(undefined)
当您声明变量但不初始化它时,它包含一个未定义 undefined 的值。但是,您也可以手动为变量分配未定义的值。
<html>
<head>
<title> JavaScript Undefined </title>
</head>
<body>
<script>
let houseNo; // 包含未定义的值
let apartment = "Ajay";
apartment = undefined; // 分配未定义的值
document.write("The value of the house No is: " + houseNo + "<br/>");
document.write("The value of the apartment is: " + apartment + "<br/>");
</script>
</body>
</html>
JavaScript null
当任何变量的值未知时,您可以使用 null。最好对空值或未知值使用 null,而不是 undefined 值。
<html>
<head>
<title> JavaScript null </title>
</head>
<body>
<script>
let houseNo = null; // Unknown house number
let apartment = "B-2";
appartment = null; // Updating the value to null
document.write("The value of the houseNo is: " + houseNo + "<br/>");
document.write("The value of the apartment is: " + apartment + "<br/>");
</script>
</body>
</html>
JavaScript Bigint
JavaScript 仅存储 64 位长的浮点数。如果要存储非常大的数字,则应使用 Bigint.您可以通过将 n 附加到数字末尾来创建 Bigint。
<html>
<head>
<title> JavaScript Bigint </title>
</head>
<body>
<script>
let largeNum = 1245646564515635412348923448234842842343546576876789n;
document.write("The value of the largeNum is " + largeNum + "<br/>");
</script>
</body>
</html>
JavaScript 符号(Symbol)
Symbol 数据类型是在 ES6 版本的 JavaScript 中引入的。它用于创建唯一的原始值和不可变的值。
Symbol() 构造函数可用于创建唯一元件,您可以将字符串作为 Symbol() 构造函数的参数传递。
例在下面的示例中,我们为同一字符串创建了 sym1 和 sym2 符号。之后,我们比较了 sym1 和 sym2 的值,它给出了 false 输出。这意味着这两个符号都是唯一的。
<html>
<head>
<title> JavaScript Symbol </title>
</head>
<body>
<script>
let sym1 = Symbol("123");
let sym2 = Symbol("123");
let res = sym1 === sym2;
document.write("Is sym1 and Sym2 are same? " + res + "<br/>");
</script>
</body>
</html>
JavaScript 对象(Object)
在 JavaScript 中,对象数据类型允许我们以键值格式存储数据集合。有多种方法可以定义对象,我们将在 对象 一章中看到。
在这里,我们将使用对象字面量创建一个对象。
例在下面的示例中,我们使用 '{}' (Object 字面量) 来创建一个 obj 对象。该对象包含具有字符串值的 'animal' 属性、具有数字值的 'legs' 属性,并且 'color' 变量的值被分配给 'hourseColor' 属性。
JSON.stringify() 方法将对象转换为字符串,并在输出中显示它。
<html>
<head>
<title> JavaScript Object </title>
</head>
<body>
<script>
let color = "Brown";
const obj = {
animal: "Hourse",
legs: 4,
hourseColor: color
}
document.write("The given object is: " + JSON.stringify(obj) + "<br/>");
</script>
</body>
</html>
JavaScript 数组(Array)
在 JavaScript 中,数组是不同数据类型的元素列表。您可以使用两个方括号 '[]' 创建一个数组,并在数组中插入多个逗号分隔的值。
<html>
<head>
<title> JavaScript Array </title>
</head>
<body>
<script>
const colors = ["Brown", "red", "pink", "Yellow", "Blue"];
document.write("The given array is: " + colors + "<br/>");
</script>
</body>
</html>
JavaScript 日期(Date)
您可以使用 JavaScript Date 对象来操作日期。
例在下面的示例中,我们使用 Date() 构造函数创建日期。在输出中,您可以根据自己的时区查看当前日期和时间。
<html>
<head>
<title> JavaScript Date </title>
</head>
<body>
<script>
let date = new Date();
document.write("The today's date and time is: " + date + "<br/>");
</script>
</body>
</html>
动态类型
JavaScript 是一种动态类型语言,类似于 Python 和 Ruby。因此,它在运行时决定变量的数据类型,而不是在编译时决定。我们可以初始化或重新分配任何数据类型的值给 JavaScript 变量。
例在下面的示例中,我们使用 string 值初始化了第一个变量。之后,我们将其值更新为 number 和 boolean 值。
<html>
<head>
<title> JavaScript dynamic data type </title>
</head>
<body>
<script>
let first = "One"; // it is string
first = 1; // now it's Number
document.write("The value of the first variable is " + first + "<br/>");
first = true; // now it's Boolean
document.write("The value of the first variable is " + first + "<br/>");
</script>
</body>
</html>
使用 typeof 运算符检查数据类型
typeof 运算符允许您检查变量的类型。
例在下面的示例中,我们使用 typeof 运算符来检查各种变量的数据类型。
<html>
<head>
<title> typeof operator </title>
</head>
<body>
<script>
let num = 30;
let str = "Hello";
let bool = true;
document.write("The data type of num is: " + typeof num + "<br/>");
document.write("The data type of str is: " + typeof str + "<br/>");
document.write("The data type of bool is: " + typeof bool + "<br/>");
</script>
</body>
</html>