在Javascript中空字符串求值为false的原因是什么 [英] What is the reason that an empty string evaluates to false in Javascript

查看:154
本文介绍了在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

这篇关于在Javascript中空字符串求值为false的原因是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆