在javascript中空字符串被视为falsy吗? [英] Is empty string treated as falsy in javascript?

查看:84
本文介绍了在javascript中空字符串被视为falsy吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到如果你有以下声明:

I've noticed that if you have a statement as the following:

var test = "" || null

test 将评估为 null 但如果我们执行以下操作:

test will evaluate to null but if we do something like the following:

var test = "test" || null

test 将评估为test ,对于代替字符串的任何对象也是如此,因此javascript将空字符串视为falsy或null值,如果是,为什么呢?不是一个空字符串仍然是一个对象,所以它不应该被处理相同吗?

test will evaluate to "test", the same holds true for any object in place of the string, so does javascript treat empty string as either a falsy or null value, and if so why? Isn't an empty string still an object, so shouldn't it be handled the same?

我已经在FireFox,Chrome,IE7 / 8 /中进行了测试9,和Node。

I've tested this out in FireFox, Chrome, IE7/8/9, and Node.

推荐答案


javascript是否将空字符串视为falsy或null值,如果是这样,为什么?

Does javascript treat empty string as either a falsy or null value, and if so why?

是的,是因为规范如此说(§9.2)


不是空的字符串仍然是一个对象

Isn't an empty string still an object

否。原始字符串值不是对象,只有新字符串()将是(并且将是真实的)

No. An primitive string value is no object, only a new String("") would be (and would be truthy)

这篇关于在javascript中空字符串被视为falsy吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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