JS变量类型判断

作者: GongAo_4 | 来源:发表于2019-04-09 15:48 被阅读0次

    typeof

    console.log(typeof coke)//coke为变量,var coke=123;
    

    从输出的结果来看,arr, json, nul, date, reg, error 全部被检测为object类型,其他的变量能够被正确检测出来。当需要变量是否是number, string, boolean, function, undefined, json类型时,可以使用typeof进行判断。其他变量是判断不出类型的,包括null。还有,typeof是区分不出array和json类型的。因为使用typeof这个变量时,array和json类型输出的都是object。

    instanceof

    相关文章

      网友评论

        本文标题:JS变量类型判断

        本文链接:https://www.haomeiwen.com/subject/ajxiiqtx.html