JS:typeof
阅读原文时间:2023年07月08日阅读:1

想要弄明白某一个变量中保存的数据到底是什么数据类型,我们可以使用到typeof操作符。

typeof操作符:检测变量的数据类型。

看例子!

    var a = "abc";  
    var b = 1;  
    var c = true;  
    var d = null;  
    var e;  //vra e = undefined;  
    var f = function (){};  
    var g = {name:"lili"};  
    var h = \[1,2,2,3\];

    console.log(typeof(a));     //string  
    console.log(typeof(b));     //number  
    console.log(typeof(c));     //boolean  
    console.log(typeof(d));     //object  
    console.log(typeof(e));     //undefined  
    console.log(typeof(f));     //function  
    console.log(typeof(g));     //object  
    console.log(typeof(h));     //object    

一个疑问:

为什么typeof(null)的结果为object?