Is empty string treated as falsy in javascript?

后端 未结 5 1753
渐次进展
渐次进展 2021-01-17 01:58

I\'ve noticed that if you have a statement as the following:

var test = \"\" || null

test will evaluate to null b

5条回答
  •  花落未央
    2021-01-17 02:30

    One dangerous issue of falsey values you have to be aware of is when checking the presence of a certain property.

    Suppose you want to test for the availability of a new property; when this property can actually have a value of 0 or "", you can't simply check for its availability using

     if (!someObject.someProperty)
        /* incorrectly assume that someProperty is unavailable */
    In this case, you must check for it being really present or not:
    
    if (typeof someObject.someProperty == "undefined")
        /* now it's really not available */
    

    SEE HERE

提交回复
热议问题