最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Jun 8, 2020 at 7:02 PhoneixS 11k8 gold badges61 silver badges74 bronze badges asked Jan 1, 2012 at 11:58 cc youngcc young 20.2k32 gold badges94 silver badges150 bronze badges 6
  • Are you sure, I am getting false here: jsfiddle.net/8CKbd – anubhava 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. – Azat 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. – PhoneixS Commented May 9, 2019 at 16:19
  • @PhoneixS edited, hope it's more meaningful now. – SwissCoder 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. – PhoneixS Commented Jun 8, 2020 at 7:04
 |  Show 1 more comment

6 Answers 6

Reset to default 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

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)

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

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

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

Yes, you can rely on that behavior.

A shorter way to write it will be:

var b = !!a;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论