In javascript, is an empty string always false as a boolean?
var a = '';
var b = (a) ? false : true; // fixed!
console.log(b); // => true
var
b
will be set totrue
.is this a defined behavior that can be relied upon?
As answered above, yes, that is the defined behavior of an empty string in a conditional (an if
expression, ||
, &&
, ? :
, ...). (The standard says that the internal ToBoolean operation must be applied.)
The evaluation is different when the empty string is used in a comparison (see Truth, Equality and JavaScript), even though the results are mostly the same:
// conditional (note: evaluation to false prints false here!)
console.log('' ? true : false); // zero length => false
// comparisons
console.log('' == true); // +0 === 1 => false
console.log('' == false); // +0 === +0 => true
console.log('' === true); // different types => false
console.log('' === false); // different types => false
Explanation: Essentially, when the operands of ==
have different types, JavaScript tries hard to convert them to Numbers, according to their value, (using operations the standard calls ToNumber and ToPrimitive), and then it internally applies ===
. But when you use ===
directly, the types are not converted, so comparing a String to a Boolean is always false
.
Roughly speaking, JavaScript conditionals (ToBoolean) test for a defined, non-null, non-zero, non-empty, non-false value (an empty String is ... empty, the Numbers -0 or +0 are ... zero, NaN is not a defined number, but an empty Object is apparently not really empty), or as I like to think, conditionals test for a (true) thing, while ==
compares the apparent, carefully converted values (ToPrimitive, ToNumber) of its operands, and ===
looks for exact sameness.
if (X) {} // is X a (true) thing?
if (X == Y) {} // are the values of X and Y same-ish?
if (X === Y) {} // are X and Y exactly the same?
There are more examples in Truth, Equality and JavaScript where this distinction really matters, e.g. '0'
is true
in a conditional (non-zero length, or, it is a thing), but false
in a ==
comparison (the value is zero). '1'
again, is true
in both cases (it is a thing and has a non-zero value).
console.log('' ? true : false); // zero length => false
console.log('' == true); // +0 === 1 => false
console.log('0' ? true : false); // non-zero length => true
console.log('0' == true); // +0 === 1 => false
console.log('1' ? true : false); // non-zero length => true
console.log('1' == true); // 1 === 1 => true
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
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
)