js 判断变量是否为空或未定义

判断变量是否定义:

if(typeof(hao) == "undefined"){
    //未定义
}else{
    //定义
}

判断变量是否为空或NULL,是则返回'', 反之返回原对象值:

function getStr(data){
    if(!data){
        return '';
    }else if(typeof(data) == "undefined"){
        return '';
    }

    return data.toString();
}

猜你喜欢

转载自www.cnblogs.com/scott-j/p/9055388.html