In javascript, is an empty string always false as a boolean?

JavascriptStringBoolean

Javascript Problem Overview


in javascript,

var a = '';
var b = (a) ? true : false;

var b will be set to false.

is this a defined behavior that can be relied upon?

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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)

Solution 3 - Javascript

var a = '';
var b = (a) ? false : true;   // fixed!
console.log(b);               // => true

> var b will be set to true. > > 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][1] 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][2]), 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][3] [operations][4] 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][2] 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

[1]: http://ecma262-5.com/ELS5_HTML.htm#Section_11.12 "11.12 Conditional Operator ( ? : )" [2]: https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/ "Truth, Equality and JavaScript" [3]: http://ecma262-5.com/ELS5_HTML.htm#Section_11.9.1 "11.9.1 The Equals Operator ( == )" [4]: http://ecma262-5.com/ELS5_HTML.htm#Section_11.9.3 "11.9.3 The Abstract Equality Comparison Algorithm"

Solution 4 - Javascript

var b will be set to true. This is because an empty string counts as a 'falsey' value in JavaScript as do some other values.

Please look at http://www.sitepoint.com/javascript-truthy-falsy/ for falsy values

Solution 5 - Javascript

> Examples of expressions that can be converted to false are those that > evaluate to null, 0, the empty string (""), or undefined. > (see MDN Reference)

Solution 6 - Javascript

Yes, you can rely on that behavior.

A shorter way to write it will be:

var b = !!a;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncc youngView Question on Stackoverflow
Solution 1 - JavascriptArenielleView Answer on Stackoverflow
Solution 2 - JavascriptHosseinView Answer on Stackoverflow
Solution 3 - JavascriptOrafuView Answer on Stackoverflow
Solution 4 - JavascriptJonathon BolsterView Answer on Stackoverflow
Solution 5 - JavascriptMarcel JackwerthView Answer on Stackoverflow
Solution 6 - JavascriptJöckerView Answer on Stackoverflow