标题:JavaScript 中 操作符“” 和“=” 的区别

记录一些很坑的区别:

1.

'' == '0'           // false
0 == '' // true
0 == '0' // true false == 'false' // false
false == '0' // true false == undefined // false
false == null // false
null == undefined // true ' \t\r\n ' == 0 // true

2.

var a = [1,2,3];
var b = [1,2,3]; var c = { x: 1, y: 2 };
var d = { x: 1, y: 2 }; var e = "text";
var f = "te" + "xt"; a == b // false
a === b // false c == d // false
c === d // false e == f // true
e === f // true

3.

"abc" == new String("abc")    // true
"abc" === new String("abc") // false

4.

5.

11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows: 1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
4. If Type(x) is not Number, go to step 11.
5. If x is NaN, return false.
6. If y is NaN, return false.
7. If x is the same number value as y, return true.
8. If x is +0 and y is −0, return true.
9. If x is −0 and y is +0, return true.
10. Return false.
11. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
12. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false.

6.两张神图

JavaScript 类型转换

转换为字符串 数字 布尔值 对象
undefined "undefined" NaN false throws TypeError
null "null" 0 false throws TypeError
true "true" 1 new Boolean(true)
false "false" 0 new Boolean(false)
""(空字符串) 0 false new String("")
"1.2"(非空,数字) 1.2 true new String("1.2")
"one"(非空,非数字) NaN true new String("one")
o "0" false new Number(0)
-0 "0" false new Number(-0)
NaN "NaN" false new Number(NaN)
Infinity "Infinity" true new Number(Infinity)
-Infinity "-Infinity" true new Number(-Infinity)
1(无穷大,非零) "1" true new Number(1)
{}(任意对象) 后续讲解 后续讲解 true
[] (任意数组) "" 0 true
[9] (一个数字元素) "9" 9 true
['a'] (其他数组) 使用join() 方法 NaN true
function(){}( 任意函数) 后续讲解 NaN true

参考链接:https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons

坑:JavaScript 中 操作符“==” 和“===” 的区别的更多相关文章

  1. JavaScript中+操作符的特殊性

    在JavaScript中+操作符有两个作用: (1)加法运算 (2)字符串连接 在使用+操作符进行运算时,当+操作符两边都是数值类型的时候,进行加法运算; 当+操作符两边有任意一边是字符串,则进行字符 ...

  2. javaScript中"=="和"==="运算符的区别

    相同点: 两个运算符均可用于比较两个值是否相等,可允许操作任意类型的操作数,如果操作数相等则返回true,否则返回false. 不同点: "==="运算符也称为严格相等运算符,它用 ...

  3. Javascript中valueOf与toString区别

    前言 基本上,所有JS数据类型都拥有这两个方法,null除外.它们俩解决JavaScript值运算与显示的问题,重写会加大它们调用的优化. 测试分析 先看一例:var aaa = {  i: 10,  ...

  4. javascript中in和hasOwnProperty区别

    in操作符只要通过对象能访问到属性就返回true.hasOwnProperty()只在属性存在于实例中时才返回true. function Person(){ } Person.prototype.n ...

  5. javascript中=、==与===的区别

    1.等号 =赋值运算符,给变量赋值 var a="1"; 2.相等和不相等操作符 相等操作符由==表示,若两个操作数相等,则返回true:不相等操作符由!=表示,若两个操作数不相等 ...

  6. javascript 中 undefined 和 null 区别

    1.相同点 如果我们直接用 undefined == null  比较他们是相等的返回的将是 true. 2.区别 当我们用undefined === null 比较的时候最后返回的将是 false. ...

  7. javascript中apply,call,bind区别,bind兼容等问题总结

    1 三者的相似之处: (1).都是用来改变函数的this对象的指向的 (2).都是用第一个参数来做this对象的指向 (3).都可以传参数进去 那么,具体到它们有什么区别呢?请看下面的例子: 两个对象 ...

  8. JavaScript中Null和undefind区别

    公众号原文 Javascript有5种基本类型:Boolean,Number,Null,Undefined,String:和一种复杂类型:Object(对象): undefined:只有一个值,及特殊 ...

  9. 【JS】JavaScript中Null和undefind区别

    1.undefined:只有一个值,及特殊的undefined.在使用var声明变量但未对其初始化时,这个变量的值是undefined,简言之,undefined就是表示变量申明了但未初始化时的值. ...

随机推荐

  1. CountDownLatch 使用说明

    CountDownLatch是一种java.util.concurrent包下一个同步工具类,它允许一个或多个线程等待直到在其他线程中一组操作执行完成. CountDownLatch的用法非常简单,下 ...

  2. Leetcode 4——Partition List

    Problems: Given a linked list and a value x, partition it such that all nodes less than x come befor ...

  3. JavaScript(第二天)【语法,变量】

    一.语法构成 区分大小写 ECMAScript中的一切,包括变量.函数名和操作符都是区分大小写的.例如:text和Text表示两种不同的变量.   标识符 所谓标识符,就是指变量.函数.属性的名字,或 ...

  4. 北京工业大学耿丹学院2016下C作业学习总结

    北京工业大学耿丹学院2016下C的班级地址在https://edu.cnblogs.com/campus/bjgygd/Sixteen-One . 第一次作业:两部分 第一部分:新建博客,书写第一篇随 ...

  5. android数据库持久化框架, ormlite框架,

    前言 Android中内置了SQLite,但是对于数据库操作这块,非常的麻烦.其实可以试用第3方的数据库持久化框架对之进行结构上调整, 摆脱了访问数据库操作的细节,不用再去写复杂的SQL语句.虽然这样 ...

  6. 利用Python爬取新浪微博营销案例库并下载到本地

    from bs4 import BeautifulSoup import requests,urllib.request,urllib.parse import json import time im ...

  7. 第十条:始终要覆盖toString()方法

    Object类提供的toString()方法如下: public String toString() {    return getClass().getName() + "@" ...

  8. python 面向对象设计思想发展史

    这篇主要说的是程序设计思想发展历史,分为概述和详细发展历史 一,概述 1940年以前:面向机器 最早的程序设计都是采用机器语言来编写的,直接使用二进制码来表示机器能够识别和执行的 指令和数 据.简单来 ...

  9. 关于 Ubuntu Linux 16.04中文版的 root 权限及桌面登录问题

    新接触 Ubuntu 的朋友大多会因为安装中没有提示设置 root 密码而不太清楚是什么原因. 起初 Ubuntu 团队希望安装尽可能的简单. 不使用 root , 在安装期间的两个用户交互步骤可以省 ...

  10. java:多层文件夹情况下,判断文件夹下是否有文件夹,并获取到没有文件夹的名字的方法

    业务问题案例 在公司遇到的一个问题,本以为很小很好解决,没想到花了一下午时间.图给的是文件路径,page1下有10个文件夹,每个有的有文件夹或者文件,要求得到page1下(即:123456789,10 ...