如何在 JavaScript 中检查变量或对象的类型?

javascriptweb developmentfront end technology更新于 2024/6/4 13:16:00

JavaScript 是一种松散类型的编程语言,这意味着没有声明变量类型的规则。变量可以在程序中存储多种数据类型,因此在使用变量之前了解变量类型至关重要。在 JavaScript 中,我们可以使用 typeof 运算符来检查变量或对象的类型。typeof 运算符接受一个变量并以字符串格式返回其类型。

除了 typeof 运算符之外,JavaScript 还提供了 instanceof 运算符来检查变量或对象的类型。instanceof 运算符接受两个参数:要检查的对象和要检查的类型的构造函数。如果构造函数是对象类型,则运算符将返回 true。

使用 typeof 运算符

typeof 运算符是一个一元运算符,它接受一个参数并返回一个表示该参数类型的字符串。例如,typeof 运算符可用于检查变量或对象的类型。

语法

typeof variable_name

在上述语法中,variable_name 是要确定其类型的变量的名称。

typeof 运算符可以返回以下字符串之一 -

  • "number"表示数字

  • "string"表示字符串

  • "boolean"表示布尔值

  • "undefined"表示未定义值

  • "object"表示对象(包括数组和函数)

  • "symbol"表示符号(ECMAScript 2015 中的新增功能)

示例

在此示例中,我们使用 typeof 运算符检查 JavaScript 中变量或对象的类型。我们声明多个不同类型的变量,例如数字、字符串、布尔值等。我们在网页上显示了这些变量。我们在按钮上使用点击事件处理程序来检查变量的类型。用户单击按钮时,即可在网页上看到所有变量及其各自的类型。typeof 运算符有助于在执行特定操作之前确定变量或对象的类型。例如,您可以使用它来确保变量在执行算术之前是数字,或者在将变量与另一个字符串连接之前是字符串。

<html>
<body>
   <h2>Checking the <i> type of a variable or object </i> in JavaScript</h2>
   <h4>The variables are as follows:</h4>
   <ul>
      <li>let num = 10</li>
      <li>let str = "Hello"</li>
      <li>let bool = true</li>
      <li>let un</li>
      <li>let n = null</li>
      <li>let arr = [1, 2, 3]</li>
      <li>let func = function () {}</li>
   </ul>
   <button onclick = "checkType()"> Check Type </button>
   <div id = "root"> </div>
   <script>
      let num = 10
      let str = 'Hello'
      let bool = true
      let un
      let n = null
      let arr = [1, 2, 3]
      let func = function () {}
      let root = document.getElementById('root')
      function checkType() { 
         root.innerHTML = '<h4>The types of variables are as follows:</h4>'
         root.innerHTML += '<ul> <li> let num = 10 is a ' + typeof num + ' </li> <li> let str = "Hello" is a ' + typeof str + ' </li> <li> let bool = true is a ' + typeof bool + ' </li> <li> let un is a ' + typeof un + ' </li> <li> let n = null is a ' + typeof n + ' </li> <li> let arr = [1, 2, 3] is a ' + typeof arr + ' </li> <li> let func = function () {} is a ' + typeof func + ' </li> </ul> '
      }
   </script>
</body>
</html> 

使用 instanceof 运算符

在 JavaScript 中,instanceof 运算符用于在运行时确定对象的类型。它返回一个布尔结果,指示对象是否是特定类的实例。

语法

object_name instanceof object_constructor

在上述语法中,object_name 是要确定其类型的对象的名称。

示例

在此示例中,我们使用 instanceof 运算符检查 JavaScript 中变量或对象的类型。我们声明一个字符串类型变量,其中包含一个 String 类构造函数和一个自定义类对象"myClassObject"(它是"myClass"的对象),并在网页上显示它们。我们在按钮上使用点击事件处理程序来检查对象的类型并将它们显示在网页上。

<html>
<body>
   <h2>Checking the <i> type of a variable or object </i> in JavaScript</h2>
   <h4>The object variables are as follows:</h4>
   <ul> 
      <li>let str = new String('Hello World!')</li>
      <li>let myClassObject = new MyClass()</li>
   </ul>
   <button onclick = "checkType()"> Check Type </button>
   <div id = "root"> </div>
   <script>
      let str = new String('Hello World!')
      class MyClass {}
      let myClassObject = new MyClass()
      let root = document.getElementById('root')
      function checkType() {
         root.innerHTML = '<h4> The types of objects using instanceof operator: </h4>'
         root.innerHTML += '<ul> <li> str is an instance of String: ' + (str instanceof String) + ' </li> <li> str is an instance of MyClass: ' + (str instanceof MyClass) + ' </li> </ul>'
         root.innerHTML += ' <ul> <li> myClassObject is an instance of String: ' + (myClassObject instanceof String) + ' </li> <li> myClassObject is an instance of MyClass: ' + (myClassObject instanceof MyClass) + ' </li> </ul>'
      }
   </script>
</body>
</html> 

typeof 和 instanceof 运算符有时仅在与某些对象一起使用时才返回预期结果。例如,typeof 运算符对数组返回"object",即使它们是 JavaScript 中的对象类型。要正确检查值是否为数组,您可以使用 Array.isArray() 方法。


相关文章