js判断是否为空

//节点上直接判断是否为undefined

<img ng-src="
{{items.thumbnail?
    items.thumbnail:
        '../img/zhishileibiaomorentu.png'}}" 
onerror="this.src='../img/zhishileibiaomorentu.png'" ng-cloak/>                            

//注意:items.thumbnail为undefined时ng-src不会报错,
//所以要加判断,不能直接写items.thumbnail。   

判断a是否为空的方法:

注意:0,“”,undefined、null都可用这样方法判断,
因为空串、null、0、undefined它们都是false。

a ? true : false

实例:

 /*---------------判断undefined和为空:console.log(a ? true : false);//false*/
                console.log(undefined);//undefined
                console.log('undefined');//undefined
                console.log(undefined ? true : false);//false
                console.log('undefined' ? true : false);//true
                console.log("" ? true : false);//false
                console.log("a" ? true : false);//true
                console.log(null ? true : false);//false
                console.log(0 ? true : false);//false
                console.log(-1 ? true : false);//true
                console.log(1 ? true : false);//true

                console.log("-----如果是空串或者未定义-----------");
                var a = "";
                console.log(a);//空白
                console.log(a ? true : false);//false
                console.log(a.length != 0 ? true : false);//false
                console.log(a || a.length != 0 ? true : false);//false || false = false
                console.log(a && a.length != 0 ? true : false);//false && false = false

                console.log("--------------------------如果有不为空的值-----------------------");
                var b = "bbbb";
                console.log(b ? true : false);//true
                console.log(b.length != 0 ? true : false);//true
                console.log(b || b.length != 0 ? true : false);//true || true = true
                console.log(b && b.length != 0 ? true : false);//true && true = true

                /*------------------------------------------------------------------------------------------*/

判断输入框是否为空:

  function isnull(val) {

        var str = val.replace(/(^s*)|(s*$)/g, '');//去除空格;

        if (str == '' || str == undefined || str == null) {
            //return true;
            console.log('空')
        } else {
            //return false;
            console.log('非空');
        }
    }

同时判断 null、undefined、数字零、false

var exp = null;
if (!exp)
{
    alert("is null");
}

同时判断 null 和 undefined

var exp = null;
if (exp == null)
{
    alert("is null");
}

判断null

typeof exp != “undefined” 排除了 undefined;

exp != 0 排除了数字零和 false。

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

更简单的正确的方法:

var exp = null;
if (exp === null)
{
    alert("is null");
}

尽管如此,我们在 DOM 应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。

示例:

var a;//声明未初始化,没有长度
var b = undefined;//没有长度
var c = null;//没有长度
var d = 0;//长度undefined
var e = "";//长度为0
  //以上全为false,数字0和空字符串不等于null

    console.log(!a);//true
    console.log(!b);//true
    console.log(!c);//true
    console.log(!d);//true
    console.log(!e);//true

    console.log("-------------------------------")

    console.log(a==null);//true
    console.log(b==null);//true
    console.log(c==null);//true
    console.log(d==null);//----false
    console.log(e==null);//----false
    console.log(false==0);//true

参考:

JS中如何判断null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值