javascript运算符_JavaScript中==和===运算符之间的区别

javascript运算符

JavaScript ==和===运算符 (JavaScript == and === operators)

Differentiating between the '==' and '===' operators is important as they're both quite similar to each other in terms of functionality but at the same time also very different from each other in terms of how that functionality is achieved and their use cases. In this article, we'll understand how the two are different and when to use which.

区分'=='和'==='运算符很重要,因为它们在功能上都非常相似,但同时在实现功能和方式上也彼此非常不同。他们的用例。 在本文中,我们将了解两者的区别以及何时使用它们。

Both double equalsTo ( == ) and Triple equalsTo ( === ) are used to compare variables.

两次equalsTo(==)和三次equalsTo(===)都用于比较变量。

let x = 10;
let y = 20;
console.log(x == y);

Output

输出量

false

The double equalsTo ( == ) operator is simply used to compare the values of two variables. It will return a boolean indicating the result. A truthy is the values you are comparing are equal and false otherwise. Let's look at a few examples,

double equalsTo(==)运算符仅用于比较两个变量的值。 它将返回一个指示结果的布尔值。 事实是,您要比较的值相等,否则为假。 我们来看几个例子

let s1 = "abc";
let s2 = "abc";
console.log(s1 == s2);

Output

输出量

true

Since both the strings s1 and s2 are equal, i.e., they both store the same set of characters the == operator returns true.

由于字符串s1和s2相等,即它们都存储相同的字符集,因此==运算符将返回true。

let t = 12;
let tS = "12";
console.log(t == tS);

Output

输出量

true

In the above example, we compare a string and an integer. However, since JS interprets that they both store the same value the double equalsTo (==) returns true. Since JS is a weakly type language, such results can be expected in contrary to other low-level languages like C++ where such a comparison is not allowed in the first place. Due to the weakly types nature of JS and such comparisons, there was a need for a more strict comparison operator and this is where the triple equalsTo (===) comes into place.

在上面的示例中,我们比较了字符串和整数。 但是,由于JS解释为它们都存储相同的值,因此double equalsTo(==)返回true 。 由于JS是一种弱类型语言,因此与其他低级语言(如C ++)首先不允许进行这种比较的结果相反,可以预期得到这样的结果。 由于JS的弱类型性质和此类比较,因此需要更严格的比较运算符,这就是三重equalsTo(===)出现的地方。

let t = 12;
let tS = "12";
console.log(t === tS);

Output

输出量

false

The triple equalsTo === operator makes a more strict comparison where it returns true if and only if both the variables being compared are completely identical. This means that they should have the same type and the same value.

三重equalsTo ===运算符进行更严格的比较 ,当且仅当要比较的两个变量完全相同时 ,它才返回true 。 这意味着它们应该具有相同的类型和相同的值

let array = [1, 2, 3];
let revArr = [3, 2, 1];

console.log(array == revArr);
console.log(array === revArr);

Output

输出量

false
false

Clearly, both the operators will return false as their values are not the same.

显然,两个运算符都将返回false,因为它们的值不相同。

let str = "hello world"
let para = "hello world"

console.log(str == para);
console.log(str === para);

Output

输出量

true
true

Since both, the strings have the same set of characters and they're both strings, both operators return true. We can also say that if the result of any === comparison is true, the == comparison for the same expression will also return true but vice versa is not true.

由于这两个字符串都具有相同的字符集,并且都是字符串,因此两个运算符均返回true 。 我们也可以说,如果任何===比较的结果为true ,则同一表达式的==比较也将返回true ,反之则不为true

let ob1={ name: "Mario" }
let ob2={ name: "Mario" }

console.log(ob1==ob2);
console.log(ob1===ob2);

Output

输出量

false
false

We had two objects having the same key-value pair yet both comparison results false. Why is that so?

我们有两个对象具有相同的键值对,但两个比较结果均为假。 为什么呢?

let arr1 = [1, 2, 3]
let arr2 = [1, 2, 3]

console.log(arr1 == arr2);
console.log(arr1 === arr2);

Output

输出量

false
false

Again we have two completely identical arrays yet both the comparisons results false.

同样,我们有两个完全相同的数组,但是两个比较结果均为false

The reason for the above anomaly is that we’re comparing variables that have completely different memory locations. When we compare two integers with the same value, they both point to the same memory location which holds the same values, hence both operators return true. When we take one of them to be a string, we’re comparing two different memory locations hence even though they have the same value they === returns false as they have separate addresses in the memory. With object and object types such as arrays ( we know that arrays have an object container around them), any two variables will always occupy different memory hence we get a false when we compare them.

出现上述异常的原因是,我们正在比较内存位置完全不同的变量。 当我们比较两个具有相同值的整数时,它们都指向具有相同值的相同内存位置,因此两个运算符均返回true 。 当我们将其中一个作为字符串时,我们正在比较两个不同的内存位置,因此,即使它们具有相同的值,它们===也会返回false,因为它们在内存中具有单独的地址。 对于对象和对象类型,例如数组(我们知道数组周围有一个对象容器),任何两个变量将始终占用不同的内存,因此在比较它们时会得到错误

let ob1Copy = ob1;

console.log(ob1 == ob1Copy);
console.log(ob1 === ob1Copy);
console.log(ob1Copy == ob2);

Output

输出量

true
true
false

Now since ob1Copy accesses the same memory location as ob1, we get true for both the comparisons. Can you write a deepEqual to function which takes two objects as a parameter and returns true if they both have the same value?

现在,由于ob1Copy与ob1访问相同的内存位置,因此两个比较都成立。 您是否可以编写一个deepEqual to函数,该函数将两个对象作为参数,如果两个对象的值相同,则返回true?

翻译自: https://www.includehelp.com/code-snippets/difference-between-and-operators-in-javascript.aspx

javascript运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值