js中的类型判断

    xiaoxiao2021-04-03  41

    js中对“”null undefined的判断:

    // "":空字符串(已分配内存空间,已经实例化了) // null :无值,引用为空。(未分配内存空间,尚未实例化) // undefined :未声明的变量,已声明没有赋值的变量,不存在的对象属性 if (typeof(value) == "undefined") { alert("undefined"); }

    typeof 返回的是字符串,包括以下几种类型:

    "number""string""boolean""object""function""undefined" //原始类型 undefined boolean number string //引用类型 object //函数类型 function var a; var b = true; var c = 1; var d = "Hello"; var e = new Object(); var f = null; var g = NaN; var h = undefined; console.log(typeof aa);//打印"undefined" console.log(typeof a); //打印"undefined" console.log(typeof b); //打印"boolean" console.log(typeof c); //打印"number" console.log(typeof d); //打印"string" console.log(typeof e); //打印"object" console.log(typeof f); //打印"object" console.log(typeof g); //打印"number" console.log(typeof h); //打印"undefined" var a; // 等同于:var a=undefined; var b= null; var c= NaN; console.log(a == b); //显示"true" console.log(a != b); //显示"false" console.log(c != c); //显示"true"
    转载请注明原文地址: https://ju.6miu.com/read-666035.html

    最新回复(0)