zhaoshier Blog

vuePress-theme-reco zhaoshier    2020 - 2022
zhaoshier Blog zhaoshier Blog

Choose mode

  • dark
  • auto
  • light
Home
Category
  • Browser
  • CSS
  • Javscript
  • Node.js
  • Vue
  • VueRouter
  • vue
  • Vuex
  • other
Tag
TimeLine
Link
  • VUE教程

    • Vue官方文档 (opens new window)
    • Vuex官方文档 (opens new window)
    • Vue Router官方文档 (opens new window)
    • Electron (opens new window)
    • axios (opens new window)
    • qs (opens new window)
  • React教程

    • react+dva+antd+umi项目建立 (opens new window)
  • JS教程

    • 现代JavaScript教程 (opens new window)
  • 优秀总结

    • 师傅的ProcessOn (opens new window)
    • 学习路线-掘金jsliang (opens new window)
Contact
  • GitHub (opens new window)
author-avatar

zhaoshier

44

Article

9

Tag

Home
Category
  • Browser
  • CSS
  • Javscript
  • Node.js
  • Vue
  • VueRouter
  • vue
  • Vuex
  • other
Tag
TimeLine
Link
  • VUE教程

    • Vue官方文档 (opens new window)
    • Vuex官方文档 (opens new window)
    • Vue Router官方文档 (opens new window)
    • Electron (opens new window)
    • axios (opens new window)
    • qs (opens new window)
  • React教程

    • react+dva+antd+umi项目建立 (opens new window)
  • JS教程

    • 现代JavaScript教程 (opens new window)
  • 优秀总结

    • 师傅的ProcessOn (opens new window)
    • 学习路线-掘金jsliang (opens new window)
Contact
  • GitHub (opens new window)

判断数据类型的三种方法

vuePress-theme-reco zhaoshier    2020 - 2022

判断数据类型的三种方法

zhaoshier 2021-12-20 Javascript

# 1.引言

es6中的常用数据类型有7种,分别为: undefined、null、Boolean、String、Number、Object、Symbol。在日常开发中,我们需要判断某个变量是哪一种数据类型。下面我们就介绍常用的三种判断数据类型的方法。

  • typeof xxx:能判断出undefined、Boolean、String、Number、Object、function(null为Object类型)
  • Object.prototype.toString.call(xxx):能判断出大部分类型
  • Array.isArray(xxx):判断是否为数组

# 2.举例

//undefined
console.log(typeof undefined); //undefined
console.log(Object.prototype.toString.call(undefined)); //[object Undefined]

//null
console.log(typeof null); //object
console.log(Object.prototype.toString.call(null)); //[object Null]

//String
var str = "string";
console.log(typeof str); //string
console.log(Object.prototype.toString.call(str)); //[object String]

// Boolean
var bo = "Boolean";
console.log(typeof bo); //boolean 
console.log(Object.prototype.toString.call(bo)); //[object Boolean]

// Number
var num = 3;
console.log(typeof num); //number
console.log(Object.prototype.toString.call(num)); //[object Number]

// Array
var arr = [1, 2, 3];
console.log(typeof arr); //object
console.log(Object.prototype.toString.call(arr)); //[object Array]
console.log(Array.isArray(arr)); //true

// Object
var obj = {a: 1, b: '222'};
console.log(typeof obj); //object
console.log(Object.prototype.toString.call(obj)); //[object Object]
console.log(Array.isArray(obj)); //false

//function
function fn(){}
console.log(typeof fn); //function
console.log(Object.prototype.toString.call(fn)); //[object Function]
console.log(Array.isArray(fn); //false

//symbol
let s = Symbol();
console.log(typeof s); //symbol
console.log(Object.prototype.toString.call(s)); //[object Symbol]
console.log(Array.isArray(s)); //false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# 3.总结

  • 如果要判断一个变量是否为数组,可用Array.isArray(xxx)或Object.prototype.toString.call(xxx)方法
  • 如果要判断一个变量是否为null,用Object.prototype.toString.call(xxx)方法
我是lookroot欢迎你的关注
看板娘