Javascript 在javascript中,空字符串作为布尔值总是假吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8692982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:06:34  来源:igfitidea点击:

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

javascriptstringboolean

提问by cc young

in javascript,

在 JavaScript 中,

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

var bwill be set to false.

var b将被设置为false

is this a defined behavior that can be relied upon?

这是一个可以依赖的定义行为吗?

回答by Arenielle

Yes. Javascript is a dialect of ECMAScript, and ECMAScript language specification clearly defines this behavior:

是的。Javascript 是 ECMAScript 的一种方言,ECMAScript 语言规范明确定义了这种行为:

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

引自http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

回答by Hossein

Yes. All false, 0, empty strings ''and "", NaN, undefined, and nullare always evaluated as false; everything else is true.

是的。所有false, 0, 空字符串''"", NaN, undefined, 和null总是被评估为false;其他一切都是true

And in your example, b is falseafter evaluation. (I think you mistakenly wrote true)

在您的示例中, b 是false经过评估的。(我想你写错了true

回答by Orafu

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

var bwill be set to true.

is this a defined behavior that can be relied upon?

varb将设置为true.

这是一个可以依赖的定义行为吗?

As answered above, yes, that is the defined behavior of an empty string in a conditional(an ifexpression, ||, &&, ? :, ...). (The standard saysthat the internal ToBooleanoperation must be applied.)

如上所述,yes,这是条件if表达式,||, &&, ? :, ...)中空字符串的定义行为。(标准规定必须应用内部ToBoolean操作。)

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:

在比较中使用空字符串时,评估是不同的(参见Truth, Equality 和 JavaScript),即使结果基本相同

// 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, (usingoperationsthe standard calls ToNumberand 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.

说明:从本质上讲,当操作数==有不同的类型,JavaScript的力图将其转换为数字,根据自己的价值,(使用操作的标准要求ToNumberToPrimitive),然后在内部适用===。但是当您===直接使用时,类型不会转换,因此将 String 与 Boolean 进行比较总是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, conditionalstest for a (true) thing, while ==compares the apparent, carefully converted values(ToPrimitive, ToNumber) of its operands, and ===looks for exact sameness.

粗略地说,JavaScript条件( ToBoolean) 测试定义的、非空、非零、非空、非假值(空字符串是...空,数字 -0 或 +0 是...零,NaN 不是一个定义的数字,但一个空的 Object 显然不是真的空),或者像我想的那样,条件测试一个(真)事物,同时==比较明显的、仔细转换的ToPrimitiveToNumber)它的操作数,并===寻找完全相同

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 JavaScriptwhere this distinction really matters, e.g. '0'is truein a conditional (non-zero length, or, it is a thing), but falsein a ==comparison (the valueis zero). '1'again, is truein both cases (it is a thing and has a non-zero value).

还有更多的例子真相,平等和JavaScript有此种区分真正的问题,例如'0'true在有条件(非零长度,或者,它是一个东西),但false在一个==比较(该是零)。'1'同样,true在这两种情况下都是(它是一个事物并且具有非零值)。

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

回答by Jonathon Bolster

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

var b将被设置为true。这是因为空字符串在 JavaScript 中被视为“falsey”值,其他一些值也是如此。

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

请查看http://www.sitepoint.com/javascript-truthy-falsy/以获取虚假值

回答by Marcel Hymanwerth

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

可以转换为 false 的表达式示例包括计算结果为 null、0、空字符串 ("") 或 undefined 的表达式。(见MDN 参考