简体   繁体   English

在JavaScript中测试变量是否为null,而不会导致未定义的错误

[英]Testing variable for null in javascript without causing undefined error

I am trying to display the results of various javascript null checking techniques. 我正在尝试显示各种javascript空检查技术的结果。 codepen 码笔

However when my variable is actually null It breaks and I get no html output. 但是,当我的变量实际上为null时,它中断了,并且没有HTML输出。 I get an error in the console "y is undefined". 我在控制台中收到一个错误“ y is undefined”。 I know it is undefined but I am trying to get it to go through the loop to give you each of the tests results. 我知道它是未定义的,但我正在尝试使其遍历整个循环,以便为您提供每个测试结果。 I can't seem to figure out what could be wrong. 我似乎无法弄清楚什么是错的。 Any help would be great. 任何帮助都会很棒。

//var y = "im y";

var test1 = function () {
if (typeof (y) === 'undefined') {
    return "test 1 y is undefined";
} else {
    return "test 1 y is defined";
}
};

var test2 = function () {
if (!y) {
    return "test 2 y is undefined";
} else {
    return "test 2 y is defined";
}
};

var test3 = function () {
if (y === null) {
    return "test 3 y is undefined";
} else {
    return "test 3 y is defined";
}
};

var test4 = function () {
if (y == null) {
    return "test 4 y is undefined";
} else {
    return "test 4 y is defined";
}
};

var test5 = function () {
if (typeof (y) === undefined) {
    return "test 5 y is undefined";
} else {
    return "test 5 y is defined";
}
};

var a = [test1, test2, test3, test4, test5];
var b = [test1(), test2(), test3(), test4(), test5()];
var somehtml = [];

$.each(a, function (index) {
somehtml.push('<pre>' + a[index] + '</pre>');
var x = b[index];
somehtml.push('<p>' + x + '</p>');
});
$("div#stuff").html(somehtml.join(""));

simple check like below 简单检查如下

if (y) {
alert("defined");
return "test 1 y is defined";    

} else {
alert("not defined");
 return "test 1 y is undefined";   
}

First of all, undefined != null. 首先,未定义!= null。 Second, undefined != 'undefined'. 其次,undefined!='undefined'。

Your code examples run as expected. 您的代码示例按预期运行。 I suggest you read about javascript type coercion and truthy/falsy values . 我建议您阅读有关javascript类型强制和true / falsy值的信息 Then start using "use strict"; 然后开始使用“ use strict”; at the top of your javascript files to avoid confusion. 在您的javascript文件顶部,以避免混淆。

Sorry f I am not getting it properly.I understood your issue from the comments only. 抱歉,我没有正确使用它。我仅从评论中了解您的问题。 .You are getting the error in console because you are running the other tests like 。您在控制台中收到错误,因为您正在运行其他测试,例如

if (!y) {

and

if (y === null) {

even when y is undefined , which will trigger error in your console. 即使yundefined ,也会在控制台中触发错误。

Your logic should be like, if the variable is found undefined then dont do the other tests and else do them. 您的逻辑应该是,如果发现变量undefined则不要进行其他测试,否则进行其他测试。

the test 考试

if (typeof (y) === 'undefined') {

will be true only if the variable is undefined and null values will pass through 仅当变量未定义且null值将通过时才为true

updated according to below comments 根据以下评论更新

pls see this fiddle . 请看这个小提琴 This has the idea I conveyed. 这就是我传达的想法。 Also one more suggestion would be you can think of chaining the functions instead of triggering them in a loop. 还有一个建议是,您可以考虑链接函数,而不是在循环中触发它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM