如何区分JavaScript中的不同数据类型?

更新于
2026-10-01 00:11:23
0阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计651个文字,预计阅读时间需要3分钟。

如何区分JavaScript中的不同数据类型?

1. typeof 用于判断各种数据类型,有两种写法:typeof xxx 和 typeof(xxx)。例如:typeof 2 输出 number,typeof null 输出 object,typeof {} 输出 object,typeof [] 输出 object,typeof (){} 输出 function。

1.typeof

typeof 用来判断各种数据类型,有两种写法:typeof xxx , typeof(xxx)

例如:

typeof 2 输出 number typeof null 输出 object typeof {} 输出 object typeof [] 输出 object typeof (function(){}) 输出 function typeof undefined 输出 undefined typeof '222' 输出 string typeof true 输出 boolean

这里面包含了js里面的五种数据类型 number、string、boolean、 undefined、object 和函数类型 function

2. instanceof

判断已知对象类型的方法.instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

阅读全文

本文共计651个文字,预计阅读时间需要3分钟。

如何区分JavaScript中的不同数据类型?

1. typeof 用于判断各种数据类型,有两种写法:typeof xxx 和 typeof(xxx)。例如:typeof 2 输出 number,typeof null 输出 object,typeof {} 输出 object,typeof [] 输出 object,typeof (){} 输出 function。

1.typeof

typeof 用来判断各种数据类型,有两种写法:typeof xxx , typeof(xxx)

例如:

typeof 2 输出 number typeof null 输出 object typeof {} 输出 object typeof [] 输出 object typeof (function(){}) 输出 function typeof undefined 输出 undefined typeof '222' 输出 string typeof true 输出 boolean

这里面包含了js里面的五种数据类型 number、string、boolean、 undefined、object 和函数类型 function

2. instanceof

判断已知对象类型的方法.instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

阅读全文