有没有一种很好的简单方法来检查Javascript中的变量是否具有值? [英] Is there a nice simple way to check if a variable in Javascript has a value?

查看:59
本文介绍了有没有一种很好的简单方法来检查Javascript中的变量是否具有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一下:

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

换句话说,我要检查变量是否具有定义的值(包括0或空字符串),但不是undefined或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?

推荐答案

如果您知道 myVar ,您应该可以执行以下操作:

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

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

这篇关于有没有一种很好的简单方法来检查Javascript中的变量是否具有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆