js 判断是否为空

如题所述

一般判断为空有 null值、undefined值与NaN值

    判断undefined:

    var tmp = undefined;if (typeof(tmp) == "undefined"){    alert("undefined");}

    说明:typeof 返回的是字符串,有六种可能:"number"、"string"、"boolean"、"object"、"function"、"undefined"

    判断null:

    var tmp = null;if (!tmp && typeof(tmp)!="undefined" && tmp!=0){    alert("null");}

    判断NaN:

    var tmp = 0/0;if(isNaN(tmp)){    alert("NaN");}

附上全部相等图

≅:松散等于等于检查(==), 比如: "1" == true; [] =="0"

=:全等或恒等全等检查(===)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-03-02

js 判断是否是空

/**
 * 判断是否是空
 * @param value
 */
function isEmpty(value){
if(value == null || value == "" || value == "undefined" || value == undefined || value == "null"){
return true;
}
else{
value = value.replace(/\s/g,"");
if(value == ""){
return true;
}
return false;
}
}

-----------------------------------------------------

版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可,转载时请标注 https://blog.csdn.net/qq6759/article/details/88056757

--------------------- 

作者:dongsir 董先生 

版权声明:本文为博主原创文章,转载请附上博文链接!

原文链接:

js 判断是否是空

第2个回答  2018-05-22

js 判断是否为空的代码如下:

//    var a = "";

//    var a = " ";

//    var a = null;

//    var a = undefined;

//    var a = [];

//    var a = {};

//    var a = NaN;

if(a === undefined) { // 只能用 === 运算来测试某个值是否是未定义的

console.log("为undefined");

}

if(a == null) { // 等同于 a === undefined || a === null

console.log("为null");

}

// String    

if(a == "" || a == null || a == undefined){ // "",null,undefined

console.log("为空");

}

if(!a){ // "",null,undefined,NaN

console.log("为空"); 

}

if(!$.trim(a)){ // "",null,undefined

console.log("为空");

}

// Array

if(a.length == 0){ // "",[]

console.log("为空");

}

if(!a.length){ // "",[]

console.log("为空");

}

// Object {}

if($.isEmptyObject(a)){ // 普通对象使用 for...in 判断,有 key 即为 false

console.log("为空");

}

JavaScript程序是由若干语句组成的,语句是编写程序的指令。JavaScript提供了完整的基本编程语句,它们是:

赋值语句、switch选择语句、while循环语句、for循环语句、for each循环语句、do...while循环语句、break循环中止语句、continue循环中断语句、with语句、try…catch语句、if语句(if..else,if…else if…)。

第3个回答  2017-12-26

你说的是不是是否被定义呀。变量声明了但没赋值,它是不确定类型的。

如果是是否被定义。

if(!(typeof(x) == "undefined")){
    //TODO
}

第4个回答  2017-12-26
var str;
str=document.getElementById("input");
if(str==""){
}
相似回答