12.19---05数据类型检测

62 阅读1分钟

通过 一个方法 去检测 数据类型的值是什么

语法: typeof(要检测的数据类型的值)

typeof 的问题, 不能正确检测出 null 这个类型的实际类型, 检测 null 的时候打印结果为 object

null 代表的含义是 空, 也就是空对象, 所以 typeof 会把他当成一个对象去输出

延申: 一道面试题 typeof 能正确检测所有数据类型的值吗?

不能, 因为他没有办法准确的检测出 null 的类型是什么

    var str1 = '100'
    var str2 = 100
    var str3
    var str4 = true
    var str5 = false
    var str6 = null

    console.log(typeof(str1))   // string
    console.log(typeof(str2))   // number
    console.log(typeof(str3))   // undefined
    console.log(typeof(str4))   // boolean
    console.log(typeof(str5))   // boolean
    console.log(typeof(str6))   // object