JavaScript - 数据类型



JavaScript 数据类型

JavaScript 中的数据类型指的是我们存储或操作的值的类型。编程语言最基本的特点之一就是它支持的数据类型集合。这些是在编程语言中可以表示和操作的值的类型。

JavaScript 数据类型可以分为原始类型和非原始类型(对象)。JavaScript(ES6 及更高版本)允许你使用七种原始数据类型

  • 字符串文本,例如 "This text string" 等。
  • 数字,例如 123, 120.50 等。
  • 布尔值,例如 true 或 false。
  • null
  • undefined
  • BigInt
  • Symbol

BigIntSymbol 是在 ES6 中引入的。在 ES5 中,只有五种原始数据类型。

除了这些原始数据类型外,JavaScript 还支持一种称为对象的复合数据类型。我们将在单独的章节中详细介绍对象。

对象数据类型包含 3 个子数据类型:

  • Object
  • Array
  • Date

为什么数据类型很重要?

在任何编程语言中,数据类型对于操作都很重要。

例如,以下代码生成“1010”输出。

let sum = "10" + 10;

在这里,JavaScript 引擎将第二个操作数转换为字符串,并使用 '+' 运算符将其组合在一起,而不是将它们相加。

因此,你需要确保操作数的类型正确。

现在,让我们学习每种数据类型及其示例。

JavaScript 字符串

在 JavaScript 中,字符串是一系列字符,可以使用以下 3 种不同的方式创建:

  • 使用单引号
  • 使用双引号
  • 使用反引号

示例

在下面的示例中,我们使用单引号、双引号和反引号创建了字符串。在输出中,它为所有 3 个字符串打印相同的结果。

<html>
<head>
   <title> JavaScript string </title>
</head>
<body>
   <script>
      let str1 = "Hello World!"; // Using double quotes
      let str2 = 'Hello World!'; // Using single quotes
      let str3 = `Hello World!`; // Using backticks
      document.write(str1 + "<br>");
      document.write(str2 + "<br>");
      document.write(str3 + "<br>");
   </script>
</body>
</html>

JavaScript 数字

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 布尔值

在 JavaScript 中,布尔值数据类型只有两个值:truefalse

<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 未定义

当你声明一个变量但没有初始化它时,它包含一个未定义的值。但是,你也可以手动将未定义的值赋给变量。

<html>
<head>
   <title> JavaScript Undefined </title>
</head>
<body>
   <script>
      let houseNo; // Contains undefined value
      let apartment = "Ajay";
      apartment = undefined; // Assigning the undefined value
      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表示空值或未知值,而不是未定义值。

<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 对象

在 JavaScript 中,对象数据类型允许我们以键值对格式存储数据的集合。有多种定义对象的方法,我们将在对象章节中看到。

在这里,我们将使用对象字面量创建一个对象。

示例

在下面的示例中,我们使用 '{}'(对象字面量)创建了一个 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 数组

在 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 日期

您可以使用 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 变量。

示例

在下面的示例中,我们用字符串值初始化第一个变量。之后,我们将它的值更新为数字和布尔值。

<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>
广告
© . All rights reserved.