Is empty string treated as falsy in javascript?

后端 未结 5 1800
渐次进展
渐次进展 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:38

    Yes an empty string is falsy, however new String("") is not.

    Note also that it's well possible that

    if (x) { ... }
    

    is verified, but that

    if (x == false) { ... }
    

    is verified too (this happens for example with an empty array [] or with new String("")).

提交回复
热议问题