Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中检查空/未定义/空字符串?(How to check empty/undefined/null string in JavaScript?)

I saw this thread , but I didn't see a JavaScript specific example.

(我看到了这个线程 ,但是没有看到JavaScript特定的示例。)

Is there a simple string.Empty available in JavaScript, or is it just a case of checking for "" ?

(是否有一个简单的string.Empty在JavaScript中可用,还是仅检查""一种情况?)

  ask by casademora translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you just want to check whether there's any value, you can do

(如果您只想检查是否有任何价值,可以)

if (strValue) {
    //do something
}

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).

(如果您需要专门检查是否有一个空字符串,而不是null,那么我认为使用===运算符来检查""是最好的选择(这样您就可以知道它实际上是您要与之进行比较的字符串) )。)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...