简体   繁体   English

JavaScript 中的真假与真假有什么区别?

[英]What is the difference between truthy and falsy with true and false in JavaScript?

I have a question concerning some concepts in JavaScript such as (truthy, true) and (falsy, false) .我对 JavaScript 中的一些概念有疑问,例如(truthy, true)(falsy, false)

I know the type of 1 is not true but the question is: why 1 == true ?我知道 1 的类型不true但问题是:为什么1 == true

What was the main reason of ECMAScript to consider 1 or "ghsagh" as true ? ECMAScript 将 1 或 "ghsagh" 视为true主要原因是什么?

I also cannot understand the meaning of truthy and falsy .我也无法理解truthyfalsy的含义。

What was the benefit of this consideration?!这种考虑有什么好处?!

JavaScript likes to convert values to other types implicitly whenever possible. JavaScript 喜欢尽可能隐式地将值转换为其他类型。 Because of that, when comparing booleans to other types of variables, JavaScript uses the same logic as older programming languages.因此,在比较布尔值与其他类型的变量时,JavaScript 使用与旧编程语言相同的逻辑。 A value that represents empty, null, or zero (such as 0, or "") evaluates to false, and any other value (such as 1, 5, -19, "ghsfsah", or other meaningful content) evaluates to true.表示空、空或零(例如 0 或“”)的值的计算结果为 false,而任何其他值(例如 1、5、-19、“ghsfsah”或其他有意义的内容)计算结果为 true。

Why does it do this?它为什么这样做? Well for one it allows developers a small shortcut when checking to see if a variable has content.一方面,它允许开发人员在检查变量是否有内容时有一个小捷径。 For example, if a user doesn't give input in a text field, we can easily check to see if a field is empty and prompt the user.例如,如果用户没有在文本字段中输入,我们可以轻松检查字段是否为空并提示用户。

if ( !textfield.value ) {
    alert( "Please enter something in the text box" );
}

If you need to see if something is actually true or false , you can use === .如果您需要查看某些内容true还是false ,您可以使用===

JavaScript is very flexible about the types of values it requires. JavaScript 在它需要的值类型方面非常灵活。 If JavaScript wants a boolean, it will convert whatever value you give it to a boolean.如果 JavaScript 需要一个布尔值,它会将您提供的任何值转换为布尔值。

Some values (“truthy” values) convert to true and others (“falsy” values) convert to false.某些值(“真”值)转换为真,而其他值(“假”值)转换为假。

The following values convert to, and therefore work like, false:以下值转换为 false,因此工作方式类似于 false:

  1. undefined不明确的
  2. null空值
  3. 0 0
  4. -0 -0
  5. NaN NaN
  6. "" // the empty string "" // 空字符串

All other values, including all objects (and arrays) convert to, and work like, true.所有其他值,包括所有对象(和数组)都转换为 true,并像 true 一样工作。

As an example, suppose that the variable o either holds an object or the value null .例如,假设变量o包含一个对象或值null You can test explicitly to see if o is non-null with an if statement like this:您可以使用如下 if 语句显式测试以查看 o 是否为非空:

if (o !== null){
....
}

The strict not-equal operator !== compares o to null and evaluates to either true or false.严格不等于运算符!==onull进行比较,并评估为真或假。 But you can omit the comparison and instead rely on the fact that null is falsy and objects are truthy:但是您可以省略比较,而是依赖于null为假而对象为真这一事实:

if (o){
....
}

In the first case, the body of the if will be executed only if o is not null .在第一种情况下,只有当o不为null才会执行 if 的主体。 So the code block will execute even if o is set to undefined .因此,即使 o 设置为undefined ,代码块也会执行。

The second case is less strict: it will execute the body of the if only if o is not false or any falsy value (such as null or undefined ).第二种情况不太严格:仅当o不为false或任何虚假值(例如nullundefined )时,它才会执行 if 的主体。 Which if statement is appropriate for your program really depends on what values you expect to be assigned to o .哪个 if 语句适合您的程序实际上取决于您期望分配给o If you need to distinguish null from 0 and "", then you should use an explicit comparison.如果需要将null0和 "" 区分开来,则应使用显式比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM