17

Most of the developers on my current project use a (to me) strange way to check for empty strings in ECMAScript:

if (theString.length == 0)
    // string is empty

I would normally write this instead:

if (theString == "")
    // string is empty

The latter version seems more readable and natural to me.

Nobody I asked seemed to be able to explain the advantages of version 1. I guess that at some time in the past somebody told everybody that this is the way to do it, but now that person left and nobody remembers why it should be done this way.

I'm wondering whether there is a reason why I should choose the first version over the second? Does it matter, is one version better than the other one? Is one version safer or faster for some reason?

(We actually do this in Siebel eScript which is compliant with ECMAScript Edition 4)

Thanks.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Thomas Müller
  • 15,565
  • 6
  • 41
  • 47

3 Answers3

33

I actually prefer that technique in a number of languages, since it's sometimes hard to differentiate between an empty string literal "" and several other strings (" ", '"').

But there's another reason to avoid theString == "" in ECMAScript: 0 == "" evaluates to true, as does false == "" and 0.0 == ""...

...so unless you know that theString is actually a string, you might end up causing problems for yourself by using the weak comparison. Fortunately, you can avoid this with judicious use of the strict equal (===) operator:

if ( theString === "" )
   // string is a string and is empty

See also:

Community
  • 1
  • 1
Shog9
  • 156,901
  • 35
  • 231
  • 235
  • 1
    Ok, I did some tests, comparing the three methods (.length, ==, ===) with different variables: strings, numbers, booleans and undefined. .length and === give me the same results, except that .length generates an error when I try to use it with undefined. I think I'll start using === in my own code from now on. Thanks. – Thomas Müller Dec 01 '09 at 23:35
  • @Crescent Fresh: question is largely a duplicate of the one I linked to in "See also". I didn't realize that prior to writing the answer, but after examining it I consider it adequate and don't particularly want to gain further rep from this. If anyone has an alternate solution or disputes this one, it would be helpful if they brought that information into the original question... – Shog9 Dec 02 '09 at 02:55
  • @Shog9: interesting, and rather big of you. – Crescent Fresh Dec 02 '09 at 03:39
2

The problem is that if theString is set to 0 (zero) your 2nd example will evaluate to true.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
1

The correct answer is correct and it's important to understand type checking. But as far as performance, a string compare is going to lose to .length method every time, but by a very very small amount. If you aren't interested in shaving milliseconds from your site speed, you may want to choose what is more readable / maintainable to you and , more importantly, your team.

webdevinci
  • 310
  • 3
  • 10
  • http://jsperf.com/empty-string-vs-string-length and several others... so close that it is nearly negligible, but .length will win – webdevinci Sep 14 '15 at 15:17
  • When you have more rep, this should really go as a comment to the answer... You'll get there in no time. – Stuart Siegler Sep 14 '15 at 15:40
  • ya, still not sure how to get a 'rep' on here :-p But I my ego isn't so big either, so doesn't bother me too much. Just trying to spread the knowledge to my peers – webdevinci Sep 15 '15 at 17:09
  • I asked one question, got 18 'rep points', woohoo... man, I've used this site since it came out, or at least since 2010. Never understood the rep thing. I think I got it now, but I have to come up with strange questions (or maybe I come up with some I already know the answer to?) to get a reputation. It bothers me – webdevinci Oct 11 '15 at 10:11