181

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?

6
  • Are you sure, I am getting false here: jsfiddle.net/8CKbd Commented Jan 1, 2012 at 12:04
  • I took it a step further. String with spaces is true. if (' ') {console.log('!')} but the ' ' == 0 is true. Commented Apr 30, 2015 at 18:13
  • 2
    I think that @JonH edition is wrong, the empty string '' is evaluated to false in a boolean context so if a = ''; then a ? false : true => '' ? false : true => false ? false : true => true ( because is the evaluation for a false value). I think it should be var b = (a) ? true : false; to be correct with the next statement. Commented May 9, 2019 at 16:19
  • @PhoneixS edited, hope it's more meaningful now. Commented Jun 5, 2020 at 15:09
  • 1
    @SwissCoder it wasn't that is not readable but that it outputs another answer. Now I have corrected it. Commented Jun 8, 2020 at 7:04

6 Answers 6

259

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

Sign up to request clarification or add additional context in comments.

3 Comments

Aaaaand it's settled
One caveat is that new String("") is truthy! This is because it is an object, whereas the short form "" represents the primitive value version. The same goes for new Number(0) and even new Boolean(false). It's a good reason not to use the object versions in your code, and it does mean that if (str.length) handles this edge case whereas if (str) would not.
Link in the answer is broken.
67

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)

2 Comments

null is not false, neither true, null is null. jsfiddle.net/sq1Lkpg0
@Bruno, You can perform the same test with NaN and undefined. They are not false but they are falsy. Which is what was asked.
19

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

1 Comment

At first I thought, "pssh, that's overkill, this was well-answered already." But that was well-presented and informative. Thanks.
10

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

3 Comments

You are wrong in your answer. var b actually will be set to true because it's the opposite of var a if you look at the code.
@bombek Thanks - I'll upate. Looks like the original question was edited after my answer (the values were swapped around)
So many edits in the question, it's again the other way around.
5

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

Comments

4

Yes, you can rely on that behavior.

A shorter way to write it will be:

var b = !!a;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.