In JavaScript, you can check the equality of any two objects using == or ===. Both the operators check whether the given two objects are equal or not, but in a whole different way.

在JavaScript中,可以使用=====检查任何两个对象的相等性。 两位操作员都检查给定的两个对象是否相等,但检查方式完全不同。

Below, you can see in detail how each operator checks for equality of the given two objects.

在下面,您可以详细了解每个运算符如何检查给定两个对象的相等性。

等于运算符(==) (Equality operator (==))

The equality operator (==) compares two operands to check whether they are equal or not and returns a Boolean result. It tries to convert the operand(s) before checking the equality if they are of different types.

相等运算符( == )比较两个操作数以检查它们是否相等,并返回布尔结果。 如果操作数类型不同,它将尝试在检查相等性之前转换操作数。

您可以在下面看到相等运算符( == )如何尝试比较两个操作数。 (You can see below how the equality operator (==) tries to compare the two operands.)

不同种类 (Different types)

  • If you compare a string and a number, it tries to convert the string to a number before comparison.如果您比较字符串和数字,则它会在比较之前尝试将字符串转换为数字。
  • When one operand is a boolean and another is a number, it tries to convert the boolean to a string, so true is converted to 1, and false converts to 0.

    当一个操作数是布尔值而另一个是数字时,它将尝试将布尔值转换为字符串,因此将true转换为1 ,将false转换为0

  • When one of the operands is an object, and the other is a string or a number then, it tries to convert the object to a primitive type using either valueOf() or toString().

    当一个操作数是一个对象,而另一个是字符串或数字时,它将尝试使用以下任一方法将该对象转换为原始类型 valueOf() 要么 toString()

Output for checking equality of an object and a string/number (Screenshot by author)
用于检查对象和字符串/数字是否相等的输出(作者截屏)

对象 (Objects)

When both the operands are objects, the result would be true only when both the operands refer to the same object.

当两个操作数都是对象时,仅当两个操作数都引用相同的对象时,结果才为true

空和未定义 (null and undefined)

When one operand is null, and the other is undefined, it returns true.

当一个操作数为null且另一个操作数undefined ,它返回true

Note: The null is an assigned value, but it means empty or non-existing. And undefined means that the variable is declared but is not assigned with any value. So, in case of checking the equality, they both sum up as not referring to any value.

注意: null是分配的值,但是它表示为空或不存在。 undefined表示变量已声明,但未分配任何值。 因此,在检查相等性的情况下,它们两者都总结为未引用任何值。

其他情况 (Other cases)

For all the other scenarios, the comparison is as follows.

对于所有其他方案,比较如下。

  • Strings are only equal when the characters and their sequence are the same in both the operands.

    仅当两个操作数中的字符及其顺序相同时,字符串才相等。

  • Numbers are equal if they are of the same value, except in the case of +0 and -0, they are considered equal. If either of the operands is NaN then it returns false.

    如果数字具有相同的值,则数字相等,但+0-0除外,它们被视为相等。 如果两个操作数中的任何一个为NaN则返回false

  • Boolean values are equal only if both the operands are either true or false.

    布尔值 仅当两个操作数均为truefalse时才相等。

严格相等运算符(===) (Strict Equality operator (===))

The strict equality operator compares two operands to check whether they are equal or not and returns a Boolean result. It considers the operands of different types to be different and doesn’t try to convert them to a common type before comparison.

严格相等运算符比较两个操作数以检查它们是否相等,并返回布尔结果。 它认为不同类型的操作数是不同的,并且在比较之前不会尝试将它们转换为通用类型。

您可以在下面看到严格相等运算符(===)如何比较两个操作数。 (You can see below how the strict equality operator (===) compares the two operands.)

不同种类 (Different types)

When both operands are of different types, it returns false.

当两个操作数的类型不同时,它返回false

Output for operands of different types (Screenshot by author)
输出不同类型的操作数(作者截屏)

对象 (Objects)

When both the operands are objects, it returns true only when both the operands refer to the same object.

当两个操作数都是对象时,仅当两个操作数都引用相同的对象时,它才返回true

Output for objects (Screenshot by author)
对象的输出(作者截屏)

空或未定义 (null or undefined)

When both operands are either null or undefined, it returns true.

当两个操作数均为nullundefined ,它将返回true

Output for null and undefined (Screenshot by author)
输出为null和undefined(作者截屏)

号码 (Numbers)

When either of the operands is NaN in case of a numeric comparison, it returns false.

NaN数字比较的情况下,如果两个操作数中的任何一个为NaN ,则返回false

Output for a Nan and a number (Screenshot by author)
Nan和数字的输出(作者截屏)

其他情况 (Other cases)

In all the other scenarios, the comparison is as follows.

在所有其他情况下,比较如下。

  • Numbers are equal if they have the same numeric value, except in the case of +0 and -0, they are equal.

    如果数字具有相同的数值,则数字相等,但+0-0除外。

Output for numbers (Screenshot by author)
数字输出(作者截屏)
  • Strings are equal when both operands have the same characters in the same sequence.

    当两个操作数具有相同序列相同字符时,字符串相等。

Output for strings (Screenshot by author)
字符串输出(作者截屏)
  • Boolean values are equal when both operands are either true or false.

    当两个操作数均为truefalse时,布尔值相等。

Output for boolean values (Screenshot by author)
布尔值的输出(作者截屏)

结论 (Conclusion)

Let’s summarize what you have learned in this article:

让我们总结一下您在本文中学到的知识:

  • As you have seen how the equality operator works, you need to be careful while using it, because it tries to convert the values to the same datatype before it checks for equality.正如您所看到的相等运算符如何工作一样,在使用它时需要小心,因为它会在检查相等之前尝试将值转换为相同的数据类型。
  • Using an equality operator may not lead to the results that you are expecting in some cases, which can cause unexpected problems in the functionality of your code.在某些情况下,使用相等运算符可能不会导致您期望的结果,这可能会导致代码功能出现意外问题。
  • The strict equality checks if the given objects are equal or not, by considering both their value and type.严格相等性通过同时考虑它们的值和类型来检查给定对象是否相等。
  • In the case of strict equality, you can be sure that it checks if the given two operands are equal or not without performing any background conversions.在严格相等的情况下,可以确保它在不执行任何后台转换的情况下检查给定的两个操作数是否相等。
  • So, if you need to check for equality the right way by taking into account both the value and the type of the operand, then using strict equality is the way to go.因此,如果您需要通过同时考虑操作数的值和类型来以正确的方式检查相等性,那么使用严格的相等性是可行的方法。
  • The case when you can use the equality operator without any doubt is when you are checking equality of null and undefined.

    毫无疑问,可以使用相等运算符的情况是在检查nullundefined相等性时。

资源资源 (Resources)

  • Equality in MDN web docs

    MDN Web文档中的平等

  • Strict Equality in MDN web docs

    MDN网络文档中的严格平等

  • Comparison and Equality in MDN web docs

    MDN Web文档中的比较和平等

Thanks for reading and happy learning!

感谢您的阅读和学习愉快!

普通英语JavaScript (JavaScript In Plain English)

Enjoyed this article? If so, get more similar content by subscribing to Decoded, our YouTube channel!

喜欢这篇文章吗? 如果是这样,请订阅我们的YouTube频道解码,以获得更多类似的内容

翻译自: https://medium.com/javascript-in-plain-english/whats-the-right-way-to-check-equality-in-javascript-1c799c6078b4


http://www.taodudu.cc/news/show-3221582.html

相关文章:

  • 健身和不健身_健身应用应该具备什么?
  • Failed to load config plugin:vue/essenti al to extend from.
  • php根据手机号区分地址,PHP判断手机号码归属地、获取手机号码归属地
  • 案例—手机号码归属地专业在线查询
  • 远程连接桌面黑屏解决方法(Winlogon错误的解决办法)
  • WIN10 注册表winlogon乱删除后果及系统恢复
  • win7电脑蓝屏,显示winlogon.exe错误怎么办?
  • winlogon.exe应用程序错误怎么办
  • 在winlogon桌面显示窗口
  • 电脑开机弹出winlogon.exe 应用程序错误的对话框
  • windows7 default桌面,winlogon桌面和screensaver桌面的截屏
  • Winlogon登录和GINA
  • Winlogon事件通知包
  • nt5src去除激活的winlogon
  • winlogon通用控制对话:winlogon.exe-应用程序错误
  • 1、网上找的win7 hook winlogon实现禁用CTRL+ALT+DEL
  • Windows NT WinLogon Notify(转载+修改版)
  • Windows NT WinLogon Notify
  • Winlogon
  • winlogon源码分析
  • 【超级鼠标键盘锁】之winlogon.exe进程调试
  • WinLogon登录管理和GINA简介 (转)
  • 什么是Windows登录应用程序(winlogon.exe),为什么它在我的PC上运行?
  • 华为NCE网管创建EVPL(共享VCTrunk) 路径法三步走
  • [nlp] 负采样 nce_loss
  • 华为NCE网管配置EVPL业务(共享VCTrunk)之单站法
  • 华为NCE网管配置EVPL业务(共享VCTrunk)之路径法
  • NCE loss详解
  • tf.nn.nce_loss分析
  • NCE3

在javascript中检查相等性的正确方法是什么相关推荐

  1. 如何在JavaScript中检查变量是否为整数?

    本文翻译自:How to check if a variable is an integer in JavaScript? How do I check if a variable is an int ...

  2. 如何在 JavaScript 中检查数字是正数还是负数

    如何在 JavaScript 中检查数字是正数还是负数 前几天一个月薪35k的兄弟,给我推了一个人工智能学习网站,看了一段时间挺有意思的.包括语音识别.机器翻译等从基础到实战都有,很详细,分享给大家. ...

  3. 在JavaScript中重复字符串的三种方法

    In this article, I'll explain how to solve freeCodeCamp's "Repeat a string repeat a string" ...

  4. JavaScript中的回调地狱及解决方法

    JavaScript中的回调地狱及解决方法 1.回调地狱 在使用JavaScript时,为了实现某些逻辑经常会写出层层嵌套的回调函数,如果嵌套过多,会极大影响代码可读性和逻辑,这种情况也被成为回调地狱 ...

  5. 在JavaScript中反转字符串的三种方法

    This article is based on Free Code Camp Basic Algorithm Scripting "Reverse a String" 本文基于F ...

  6. 中单引号怎么转义_在JavaScript中组合字符串的4种方法

    下面是在JavaScript中组合字符串的4种方法.我最喜欢的方法是使用模板字符串.为什么?因为它更具可读性,所以没有转义引号的反斜杠,没有笨拙的空格分隔符,也没有混乱的加号操作符 . const i ...

  7. Mybatis中使用大于小于等于的正确方法

    在mybatis中sql是写在xml映射文件中的,如果sql中有一些特殊字符的话,在解析xml文件的时候就会被转义,如若不希望被转义,那该怎么办呢? 方法一:使用特殊转义字符 例如,>=开始日期 ...

  8. JS IOS/iPhone的Safari浏览器不兼容Javascript中的Date()问题的解决方法

    JS IOS/iPhone的Safari浏览器不兼容Javascript中的Date()问题的解决方法 参考文章: (1)JS IOS/iPhone的Safari浏览器不兼容Javascript中的D ...

  9. 在JavaScript中组合字符串的4种方法

    下面是在JavaScript中组合字符串的4种方法.我最喜欢的方法是使用模板字符串.为什么?因为它更具可读性,所以没有转义引号的反斜杠,没有笨拙的空格分隔符,也没有混乱的加号操作符

最新文章

  1. 爬虫学习笔记(二十四)—— pyspider框架
  2. 计算机程序中断方式有几种,奥鹏离线作业计算机组成原理一、简答题1、请说明程序查询方式与中断方式各自的特点.2、提高存储器速度可采用哪些措施,请说出...
  3. 用法 stl_【c++】STL里的priority_queue用法总结
  4. C# Task的简单使用
  5. 快速pow算法c语言_嵌入式必知基础算法(二)
  6. 算法不会,尚能饭否之集合(Set)
  7. 语音识别准确率永远达不到100%?
  8. Python数据结构与算法(2.2)——顺序表
  9. 亚马逊AWS沙龙笔记:如何通过AWS快速发展国际业务?及多种架构方案
  10. CMD操作查看电脑IP
  11. 吴恩达机器学习系列课程笔记——第一章:什么是机器学习(Machine Learning)
  12. 富文本编辑器kindeditor支持从word复制粘贴保留格式和图片的插件
  13. java append()_append()方法的坑
  14. 怎么给照片加水印?今天教你一个方法
  15. 莫队算法 (普通莫队、带修莫队、树上莫队)
  16. NTKO OFFICE文档控件为何不能自动装载?
  17. python程序执行完后重头开始做烧饼_从“程序员转行卖烧饼”想到IT人创业
  18. 使用传统AV屏幕用作树莓派屏幕
  19. 你和csdn是什么关系
  20. Muli3D 8 计算Shader中顶点属性相对于屏幕坐标的偏导数

热门文章

  1. linux 删除目录下所有指定的子目录
  2. 七年级计算机的发展是教案,七年级信息技术上学期 第三课计算机的发展与应用 教案...
  3. 2020年第十二届全国大学生数学竞赛初赛(非数学)大题2-3题解析
  4. Pro/E环境下不锈钢钣金的结构设计
  5. [分享] 冒险岛079私服搭建
  6. 计算机内存多大够用,内存多大才够用?电脑内存选购指南
  7. Keynote for Mac 5.3 下载
  8. 2017年职称计算机考试教程,【2017年职称计算机考试wps教程资料2】- 环球网校
  9. C语言代码输入的规范
  10. 写个小爬虫爬下迅雷会员