Wednesday, 15 April 2015

javascript - Why should I use string.length == 0 over string == "" when checking for empty string in ECMAScript? -


Most developers on my current project use the strange method (me) to check the empty strings in ECMAScript:

  If the string is empty  

I will type it instead:

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

The later versions seem more readable and natural to me.

I asked someone to be able to explain the benefits of version 1. I think in the past few days somebody had told everyone that this is the way to do this, but now that person has left and nobody misses why he should do it like this.

> I am thinking that what is the reason that I should choose the first version on the second edition? Is it a matter, is one version better than another? Is a version safe or fast for some reason?

(We actually do this in the Siebel ESPT which correspond to ECMAScript version 4)

Thank you.

I really like that technology in many languages, because sometimes an empty string It is difficult to differentiate between "" and many other strings ( "" , '' ').

But there is another reason to avoid theString == "" false == "" and 0.0 = as true = "" ...

... as long as you address such as the string < / Code> is actually a string , you can bother yourself by using a weak comparison. Fortunately, you can avoid the fair use of this exact equivalent ( === ) operator:

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

No comments:

Post a Comment