Is empty string treated as falsy in javascript?

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

    Does javascript treat empty string as either a falsy or null value, and if so why?

    Yes it does, and because the spec says so (§9.2).

    Isn't an empty string still an object

    No. An primitive string value is no object, only a new String("") would be (and would be truthy)

提交回复
热议问题