为什么JavaScript中的''\t\\\<br/>'== false`? [英] Why is `&#39;\t\n &#39; == false` in JavaScript?

查看:119
本文介绍了为什么JavaScript中的''\t\\\<br/>'== false`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中......

In JavaScript...

'\t\n ' == false // true

我可以假设任何仅由空白字符组成的字符串被认为等于 false 。

I can assume that any string that consists solely of whitespace characters is considered equal to false in JavaScript.

根据这篇文章,我认为 false 将转换为 0 ,但无法使用Google找到提及等于 false 的空白。

According to this article, I figured that false would be converted to 0, but was unable to find mention of whitespace considered equal to false using Google.

为什么会这样?除了深入研究ECMAScript规范之外,还有一些关于这个问题的好读物吗?

Why is this? Is there some good reading on the subject besides digging into the ECMAScript spec?

推荐答案

此页面提供了规则的很好的总结。

This page provides a good summary of the rules.

按照这些规则,'\t\ n'将转换为数字( Number('\ t) \ n')==> 0 ),并将 false 转换为数字( Number(false) )==> 0 ),因此两者相等。

Going by those rules, the '\t\n ' is converted in a number (Number('\t\n') ==> 0), and the false is converted into a number (Number(false) ==> 0), and hence the two are equal.

Alex的答案也是<$的特定情况的一个很好的细分c $ c>'\t\ n'== false

重要的区别是'\t\ n' falsy。例如:

An important distinction is that '\t\n ' is not falsy. For example:

if ('\t\n ') alert('not falsy'); // will produce the alert

这篇关于为什么JavaScript中的''\t\\\<br/>'== false`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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