如何在 JavaScript 中检查空/未定义/空字符串? [英] How can I check for an empty/undefined/null string in JavaScript?

查看:17
本文介绍了如何在 JavaScript 中检查空/未定义/空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这个问题,但我没有看到 JavaScript 特定的例子.JavaScript 中是否有一个简单的 string.Empty 可用,或者只是检查 "" 的情况?

I saw this question, but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for ""?

推荐答案

如果您只想检查是否有 真实值,你可以这样做:

If you just want to check whether there's a truthy value, you can do:

if (strValue) {
    //do something
}

如果您需要专门检查 null 上的空字符串,我认为检查 "" 是您最好的选择,使用 === 运算符(这样您就知道它实际上是您要比较的字符串).

If you need to check specifically for an empty string over null, I would think checking against "" is your best bet, using the === operator (so that you know that it is, in fact, a string you're comparing against).

if (strValue === "") {
    //...
}

这篇关于如何在 JavaScript 中检查空/未定义/空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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