有没有一种简单的方法来检查 Javascript 中的变量是否有值?

发布于 2024-12-19 08:24:47 字数 204 浏览 1 评论 0原文

我想检查这一点:

if ( typeof myVar != "undefined" && myVar != null )
    ...

换句话说,我想检查变量是否具有已定义的值(包括 0 或空字符串),但不是未定义或 null,我将其解释为无值。

我每次都必须进行两部分检查还是有方便的捷径?

I want to check this:

if ( typeof myVar != "undefined" && myVar != null )
    ...

In other words, I want to check if a variable has a defined value (including 0 or an empty string), but not undefined or null, which I interpret as valueless.

Do I have to do the two-part check each time or is there a handy shortcut?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小红帽 2024-12-26 08:24:47

如果您希望允许 0"" 作为有效值,并且您希望覆盖变量的大小写,甚至可能不会被删除,但不要将 null 视为有效值value,那么你必须像这样专门检查 undefinednull

if (typeof myVar !== 'undefined' && myVar !== null) 
   ...

很多值都是 false(它们不满足 if (myVar) code> 所以你真的必须有意识地决定哪些您正在测试的所有这些都是错误的:

undefined
false
0
""
null
NaN

如果您想允许一些,但不允许其他,那么您必须进行比 if (myVar) 更具体的测试,就像我上面所示的那样。仅隔离您关心的值。

这是关于错误值的好文章:http://www.sitepoint.com/javascript-truthy-falsy/

如果您知道该变量已被声明,并且您只想查看它是否已使用 null 以外的值进行初始化。 ,您可以使用这个:

if (myVar != undefined) 
   ...

仅使用 != 而不是 !== 允许通过类型转换测试 undefined 和 null。不过,我不建议这样做,因为如果你试图辨别错误值,最好不要让 JS 引擎进行任何类型转换,这样你就可以准确地控制它所做的事情,而不必记住所有类型转换相等规则。我坚持这一点更明确:

if (typeof myVar !== 'undefined' && myVar !== null) 
   ...

如果你想知道它是否有任何非错误值,你当然可以这样做(但这不允许 0"" 作为有效值:

if (myVar) 
   ...

If you want to allow 0 and "" as valid values and you want to cover the case of the variable might not even be delcared, but don't consider null a valid value, then you have to specifically check for undefined and null like this:

if (typeof myVar !== 'undefined' && myVar !== null) 
   ...

A lot of values are falsey (they don't satisfy if (myVar) so you really have to conciously decide which ones you're testing for. All of these are falsey:

undefined
false
0
""
null
NaN

If you want to allow some, but not others, then you have to do a more specific test than if (myVar) like I've shown above to isolate just the values you care about.

Here's a good writeup on falsey values: http://www.sitepoint.com/javascript-truthy-falsy/.

If you know the variable has been declared and you just want to see if it's been initialized with something other than null, you can use this:

if (myVar != undefined) 
   ...

Using only the != instead of !== allows this to test for both undefined and null via type conversion. Although, I wouldn't recommend this because if you're trying to discern between falsey values, it's probably better to NOT let the JS engine do any type conversions at all so you can control exactly what it does without having to memorize all the type conversion equality rules. I'd stick with this to be more explicit:

if (typeof myVar !== 'undefined' && myVar !== null) 
   ...

If you want to know if it has any non-falsey value, you can of course do this (but that won't allow 0 or "" as valid values:

if (myVar) 
   ...
蝶…霜飞 2024-12-26 08:24:47

2 部分方法。如果您不首先检查 typeof,您最终会遇到引用错误。

The 2-part method. If you don't check for the typeof first, you'll end up with a reference error.

找个人就嫁了吧 2024-12-26 08:24:47

如果您知道 myVar上下文,您应该能够执行以下操作:

if (this.myVar != null) {
    ...
}

If you know the context of myVar, you should be able to do this:

if (this.myVar != null) {
    ...
}
£冰雨忧蓝° 2024-12-26 08:24:47

如果 myvar 可能未定义,则在不进行 typeof 检查的情况下调用它将会引发错误。

如果它被定义为 null(就像 myvar= element.lastChild 对于没有子元素的元素),如果您只使用 typeof,您将错过捕获它。

if myvar could be undefined, calling it without the typeof check will throw an error.

And if it is defined as null (like myvar= element.lastChild for an element with no children) you will miss catching it if you just use typeof.

孤千羽 2024-12-26 08:24:47

好吧,null 是一个已定义的值...因此,如果您想确保该变量不包含 null,并且不是未定义的,则必须执行这两项检查。

Well, null is a defined value... so if you want to make sure that the variable doesn't contain null, and isn't undefined you must do both checks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文