首页 > web前端 > js教程 > 正文

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

WBOY
发布: 2023-09-03 10:09:02
转载
1474人浏览过

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

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

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

使用 typeof 运算符

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

语法

typeof variable_name 
登录后复制

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

立即学习Java免费学习笔记(深入)”;

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

  • “number”代表数字

  • “string”表示字符串

  • 布尔值的“boolean”

    Find JSON Path Online
    Find JSON Path Online

    Easily find JSON paths within JSON objects using our intuitive Json Path Finder

    Find JSON Path Online 193
    查看详情 Find JSON Path Online
  • “未定义”表示未定义的值

  • “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”声明一个字符串类型变量,“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() 方法。

以上就是如何在 JavaScript 中检查变量或对象的类型?的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号