Javascript 检查函数返回的值是否为空?

Javascript 检查函数返回的值是否为空?,javascript,jquery,Javascript,Jquery,这个问题(或类似问题)似乎经常被问到,但我已经尝试了很多方法来检查从我拥有的函数返回的值是否为null;无济于事 我的函数,按名称获取的URL参数: function getURLParameter(name) { return decodeURI( (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] ); } 但是,很明显,参数可能不存在,因此我需要检查返回的值 执

这个问题(或类似问题)似乎经常被问到,但我已经尝试了很多方法来检查从我拥有的函数返回的值是否为null;无济于事

我的函数,按名称获取的URL参数:

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}
但是,很明显,参数可能不存在,因此我需要检查返回的值

执行
console.log(getURLParameter('client'))
返回
null
…但执行null检查不起作用

我尝试了以下方法:

if ( getURLParameter("client") !== null ) {

    alert("It's there matey!");

}

if ( getURLParameter("client") != null ) {

    alert("It's there matey!");

}

if ( ! getURLParameter("client") ) {

    alert("It's there matey!");

}
这些似乎都不适合我


我哪里出了问题?我可以在vanilla JS中或者使用jQuery库来实现这一点。

这是因为函数返回的值是字符串(
'null'
),因此测试失败

var result = getURLParameter("client");
console.log('Value: ' + result);                // 'null'
console.log('Data type: ' + typeof(result));    // string
console.log(result!==null);                     // true
console.log(result!=null);                      // true
console.log(!result);                           // false
它返回一个字符串,因为您将
null
值作为参数传递给
decodeURI
函数,该函数将转换为字符串
'null'

因此,在调用
decodeURI
之前,我编辑了您的函数
geturlparmeter
,以检查参数的值是否为
null
。见下文:

function getURLParameter(name) {
    var param = RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || null;
    return param===null ? null : decodeURI(param[1]);
}
现在,让我们再次运行测试:

var result = getURLParameter("client");
console.log('Value: ' + result);                // null
console.log('Data type: ' + typeof(result));    // object
console.log(result!==null);                     // false
console.log(result!=null);                      // false
console.log(!result);                           // true

这是因为函数返回的值是字符串(
'null'
),因此测试失败

var result = getURLParameter("client");
console.log('Value: ' + result);                // 'null'
console.log('Data type: ' + typeof(result));    // string
console.log(result!==null);                     // true
console.log(result!=null);                      // true
console.log(!result);                           // false
它返回一个字符串,因为您将
null
值作为参数传递给
decodeURI
函数,该函数将转换为字符串
'null'

因此,在调用
decodeURI
之前,我编辑了您的函数
geturlparmeter
,以检查参数的值是否为
null
。见下文:

function getURLParameter(name) {
    var param = RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || null;
    return param===null ? null : decodeURI(param[1]);
}
现在,让我们再次运行测试:

var result = getURLParameter("client");
console.log('Value: ' + result);                // null
console.log('Data type: ' + typeof(result));    // object
console.log(result!==null);                     // false
console.log(result!=null);                      // false
console.log(!result);                           // true

问题是
decodeURI
,当您将
null
传递给它时,它返回字符串
“null”
。解决方案是在调用
decodeURI
之前执行
null
检查。您可以通过将函数分解为几个部分来找到这一点:

function getURLParameter(name) {
    var rex = RegExp(name + '=' + '(.+?)(&|$)');
    var result = rex.exec(location.search);
    var rv = decodeURI(
      (result||[,null])[1]
    );
    return rv;
}
…并在浏览器内置的调试器中遍历它

或者对于那些喜欢
控制台.log
风格调试的用户:

function getURLParameter(name) {
    var rex = RegExp(name + '=' + '(.+?)(&|$)');
    var result = rex.exec(location.search);
    console.log("typeof result = " + typeof result);
    console.log("result = " + result);
    var rv = decodeURI(
      (result||[,null])[1]
    );
    console.log("typeof rv = " + typeof rv);
    return rv;
}
…对于
getURLParameter(“foo”)
来说,它向我们显示:

typeof result = object result = null typeof rv = string 结果类型=对象 结果=空
typeof rv=string问题是
decodeURI
,当您将
null
传递给字符串时,该字符串返回字符串。解决方案是在调用
decodeURI
之前执行
null
检查。您可以通过将函数分解为几个部分来找到这一点:

function getURLParameter(name) {
    var rex = RegExp(name + '=' + '(.+?)(&|$)');
    var result = rex.exec(location.search);
    var rv = decodeURI(
      (result||[,null])[1]
    );
    return rv;
}
…并在浏览器内置的调试器中遍历它

或者对于那些喜欢
控制台.log
风格调试的用户:

function getURLParameter(name) {
    var rex = RegExp(name + '=' + '(.+?)(&|$)');
    var result = rex.exec(location.search);
    console.log("typeof result = " + typeof result);
    console.log("result = " + result);
    var rv = decodeURI(
      (result||[,null])[1]
    );
    console.log("typeof rv = " + typeof rv);
    return rv;
}
…对于
getURLParameter(“foo”)
来说,它向我们显示:

typeof result = object result = null typeof rv = string 结果类型=对象 结果=空 rv的类型=字符串尝试

试一试


相信我,有一个更好的函数可以获取URL参数(我知道这个函数来自)。您不应该每次调用函数时都需要运行正则表达式。解决这个问题所需的只是直接调试。将语句分解为各个部分,并使用浏览器中内置的调试器浏览代码。这样做比在上面输入问题要快得多。我建议看一下,但不一定喜欢接受的答案。(我更喜欢票数第二高的问题)相信我,有一个更好的函数来获取URL参数(我知道这个函数是从)。您不应该每次调用函数时都需要运行正则表达式。解决这个问题所需的只是直接调试。将语句分解为各个部分,并使用浏览器中内置的调试器浏览代码。这样做比在上面输入问题要快得多。我建议看一下,但不一定喜欢接受的答案。(我更喜欢票数第二高的问题)如果这个值实际上是“null”怎么办?
不是吗=运算符转换表达式?所以“空”和“空”没有区别。我不明白你的意思。我是说,如果查询字符串是:
?p1=null
?如果您使用了
getURLParameter(“p1”)
,结果将是
“null”
,并且您的解决方案将无法看到它……即使不是因为@Ian的非常好的观点,该测试也不会起作用:
null!='null'
返回
true
。如果该值实际为“null”?
不是吗=运算符转换表达式?所以“空”和“空”没有区别。我不明白你的意思。我是说,如果查询字符串是:
?p1=null
?如果您使用了
getURLParameter(“p1”)
,结果将是
“null”
,并且您的解决方案将无法看到它……即使不是因为@Ian的非常好的观点,该测试也不会起作用:
null!='null'
返回
true