开发者

What is the reason that an empty string evaluates to false in Javascript

开发者 https://www.devze.com 2023-03-27 05:36 出处:网络
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 surpris开发者_如何学JAVAed me:

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 surpris开发者_如何学JAVAed me:

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

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.

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

— section 9.2 of Ecma-262

0

精彩评论

暂无评论...
验证码 换一张
取 消