在Javascript中空字符串求值为false的原因是什么

问题描述:

为什么在JavaScript中空字符串的计算结果为false。我知道道格拉斯·克罗克福德在javascript中已经讲了很多关于真实和虚假的内容,但这仍让我感到惊讶:

Why is it that an empty string evaluates to false in JavaScript. I know Douglas Crockford has spoken quite a bit about truthy and falsy in javascript but this still surprised me:

if("")
    alert("you will never see this");

任何人都知道这种行为的基础是什么。是否按照ECMAScript规范以这种方式实现?我很好奇。

Anyone know what the basis of this behavior is. Is it implemented in this way according to the ECMAScript specifications? I'm curious.

因为它里面什么都没有。空字符串是许多语言中的错误值。根据规范,这是标准的。

Because it has nothing in it. An empty string is a false value in many languages. This is standard according to the specification.


如果参数为空String(其长度为零),则结果为false;
否则结果为真

The result is false if the argument is the empty String (its length is zero); otherwise the result is true

- Ecma-262