在JavaScript中,是一个空字符串总是false作为一个布尔? [英] In javascript, is an empty string always false as a boolean?

查看:185
本文介绍了在JavaScript中,是一个空字符串总是false作为一个布尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript,

in javascript,

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

变种b 将被设置为真正

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

is this a defined behavior that can be relied upon?

推荐答案

是的。 JavaScript是ECMAScript中的一种方言,和ECMAScript语言规范明确规定这种行为:

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

从<一所报价href=\"http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf\">http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

这篇关于在JavaScript中,是一个空字符串总是false作为一个布尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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