简体   繁体   English

JavaScript中“20”和20如何被认为是相同的?

[英]How is “20” and 20 considered equal in JavaScript?

I understand that using the "===" compares type, so running the following code results in "not equal" because it's comparing a number type to a string type. 我理解使用“===”比较类型,因此运行以下代码会导致“不相等”,因为它将数字类型与字符串类型进行比较。

var a = 20;
var b = "20";
    if (a === b) {
        alert("They are equal");
    } else {
        alert("They are not equal");
}

But I dont understand how using the "==" to compare only the value results in the "They are equal" message. 但我不明白如何使用“==”来比较“他们是平等的”消息中的值结果。

var a = 20;
var b = "20";
    if (a == b) {
        alert("They are equal");
    } else {
        alert("They are not equal");
}

How are the values equal? 价值如何相等? Isn't the string "20" stored as the ASCII characters 50 and 48 (0110010 and 0110000 in binary) and 20 stored as the actual binary number 0010100? 是不是字符串“20”存储为ASCII字符50和48(二进制为0110010和0110000),20存储为实际二进制数0010100?

EDIT: Thanks everyone! 编辑:谢谢大家! I think all the responses are great and have helped me understand this much better. 我认为所有的回答都很棒,并帮助我更好地理解这一点。

The == operator compares only the values of the variables. ==运算符仅比较变量的值。 If the types are different, a conversion is operated. 如果类型不同,则进行转换。 So the number 20 is converted to the string "20" and the result is compared. 因此,数字20被转换为字符串“20”并且比较结果。

The === operator compares not only the values, but also the types, so no cast is operated. ===运算符不仅会比较值,还会比较类型,因此不会运行强制转换。 In this case "20" !== 20 在这种情况下"20" !== 20

When type conversion is needed, JavaScript converts String, Number, Boolean, or Object operands as follows. 当需要类型转换时,JavaScript会按如下方式转换String,Number,Boolean或Object操作数。

  • When comparing a number and a string, the string is converted to a number value. 比较数字和字符串时,字符串将转换为数字值。 JavaScript attempts to convert the string numeric literal to a Number type value. JavaScript尝试将字符串数字文字转换为数字类型值。 First, a mathematical value is derived from the string numeric literal. 首先,从字符串数字文字中导出数学值。 Next, this value is rounded to nearest Number type value. 接下来,将此值四舍五入为最接近的Number类型值。
  • If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false. 如果其中一个操作数是布尔值,则布尔操作数如果为真则转换为1,如果为假则转换为+0。
  • If an object is compared with a number or string, JavaScript attempts to return the default value for the object. 如果将对象与数字或字符串进行比较,JavaScript会尝试返回该对象的默认值。 Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects. 运算符尝试使用对象的valueOf和toString方法将对象转换为原始值,String或Number值。 If this attempt to convert the object fails, a runtime error is generated. 如果此转换对象的尝试失败,则会生成运行时错误。

The problem with the == comparison is that JavaScript version 1.2 doesn't perform type conversion, whereas versions 1.1 and 1.3 onwards do. ==比较的问题是JavaScript版本1.2不执行类型转换,而版本1.1和1.3版本执行。

The === comparison has been available since version 1.3, and is the best way to check of two variables match. 从版本1.3开始,===比较已经可用,并且是检查两个变量匹配的最佳方法。

If you need your code to be compatible with version 1.1, 1.2 and 1.3 versions of JavaScript code, you should ensure that the variables all match as if it was an === comparison that was being performed. 如果您需要将代码与版本1.1,1.2和1.3版本的JavaScript代码兼容,则应确保所有变量都匹配,就好像它是正在执行的===比较一样。

JavaScript引擎将a视为数字,并在估值之前将b转换为数字。

Part of the definition of "==" is that the values will be converted to the same types before comparison, when possible. “==”定义的一部分是在可能的情况下,在比较之前将值转换为相同的类型。 This is true of many loosely typed languages. 许多松散类型的语言都是如此。

Javascript is designed such that a string containing numbers is considered "equal" to that number. Javascript的设计使得包含数字的字符串被认为与该数字“相等”。 The reason for that is simplicity of use for the case of users entering a number into an input field and the site validates it in JS -- you don't have to cast the entered string to a number before comparing. 原因是用户在输入字段中输入数字并且网站在JS中验证它的情况下使用简单 - 您不必在比较之前将输入的字符串强制转换为数字。

It simplifies a common use case, and the === operator still allows you to compare with the type considered as well. 它简化了一个常见的用例,===运算符仍允许您与所考虑的类型进行比较。

据我所知,JavaScript会动态进行自动数据类型转换 - 因此可能会自动将变量转换为等效类型。

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

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