In javascript, is an empty string always false as a boolean?
Solution 1:
Yes. Javascript is a dialect of ECMAScript, and ECMAScript language specification clearly defines this behavior:
ToBoolean
The result is false if the argument is the empty String (its length is zero); otherwise the result is true
Quote taken from http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
Solution 2:
Yes. All false
, 0
, empty strings ''
and ""
, NaN
, undefined
, and null
are always evaluated as false
; everything else is true
.
And in your example, b is false
after evaluation. (I think you mistakenly wrote true
)