Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript 基础系列之 typeof 能判断哪些类型 #68

Open
yuanyuanbyte opened this issue Nov 21, 2021 · 0 comments
Open

JavaScript 基础系列之 typeof 能判断哪些类型 #68

yuanyuanbyte opened this issue Nov 21, 2021 · 0 comments

Comments

@yuanyuanbyte
Copy link
Owner

yuanyuanbyte commented Nov 21, 2021

本系列的主题是 JavaScript 基础,每期讲解一个技术要点。如果你还不了解各系列内容,文末点击查看全部文章,点我跳转到文末

如果觉得本系列不错,欢迎 Star,你的支持是我创作分享的最大动力。

typeof 能判断哪些类型

typeof 能判断所有的原始类型

在这里插入图片描述

我们可以看出typeof能正确的判断出6种原始类型。不能用来判断null或者引用数据类型object(对象、数组和函数)。

对于原始数据类型,我们可以使用typeof()函数来判断他的数据类型:

console.log(typeof "");
console.log(typeof 1);
console.log(typeof true);
console.log(typeof null);
console.log(typeof undefined);
console.log(typeof []);
console.log(typeof function(){});
console.log(typeof {});

看看控制台输出什么

可以看到,typeof对于基本数据类型判断是没有问题的,但是遇到引用数据类型(如:Array)是不起作用的。

typeof 能判断是否是函数

//typeof 能判断函数
typeof console.log(1)    // function
typeof function fn () {} // function

typeof 能判断出是否是引用类型(不可细分)

//typeof 判断引用类型
const a = null       typeof a //object
const a = { a: 100 } typeof a // object
const a = ['a']      typeof a // object

查看全部文章

博文系列目录

  • JavaScript 深入系列
  • JavaScript 专题系列
  • JavaScript 基础系列
  • 网络系列
  • 浏览器系列
  • Webpack 系列
  • Vue 系列
  • 性能优化与网络安全系列
  • HTML 应知应会系列
  • CSS 应知应会系列

交流

各系列文章汇总:https://github.com/yuanyuanbyte/Blog

我是圆圆,一名深耕于前端开发的攻城狮。

weixin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant