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

javascript运算符

We can perceive the differences between these two operators as the same difference that occurs between double equalsTo (==) and triple equalsTo (===) operators. We already know that the (!) not-operator used along with (=) operator is used to check for inequalities.

我们可以将这两个运算符之间的差异视为与double equalsTo(==)和Triple equalsTo(===)运算符之间相同的差异。 我们已经知道与(=)运算符一起使用的(!)非运算符用于检查不等式。

let a = 10;
let b = 20;

console.log(a != b);

Output

输出量

true

Since both a and b hold different values, we get a truthy returned from the inequality.

由于a和b都具有不同的值,所以我们从不等式中得到了真实的结论。

let c = 50;
let d = 50;

console.log(c!=d);

Output

输出量

false

And of course in the above case since both have the same values != operator returns false. Therefore we can say that the != operator checks if the two variables being compared have the same value or hold the same value. If they don't, it returns true and false otherwise as we have seen in the above two examples.

当然,在上述情况下,因为两者都具有相同的值!=运算符将返回false 。 因此,可以说!=运算符检查要比较的两个变量是否具有相同的值或具有相同的值。 如果没有,则返回true和false,否则如上面两个示例所示。

let s = 101011;
let name = "sam";

console.log(s != name);
console.log(s !== name);

Output

输出量

true
true

We have compared two variables which are completely different type storing the same values and we get truthy for both the comparison expressions. Now, look at the next example:

我们已经比较了两个完全不同的变量,它们存储相同的值,并且两个比较表达式都正确。 现在,看下一个例子:

let a1 = 10;
let a2 = "10";

console.log(a1 !== a2);
console.log(a1 != a2);

Output

输出量

true
false

In the above comparison, we're comparing two same values due to which the != operator returns us false but we're comparing two different types due to which the !== operator returns true. Thus we can say that the !== operator not only checks for the values but also for the type of the variables being compared. Since in this case both the variables had different types, they were evaluated as unequal by the !== operator and hence we got the true result.

在上面的比较中,我们正在比较两个相同的值,因为!=运算符将其返回给我们false;但是,我们正在比较两个不同的类型,由于它们使!==运算符返回了true 。 因此,可以说!==运算符不仅检查值,还检查要比较的变量的类型。 由于在这种情况下,两个变量都具有不同的类型,因此!==运算符将它们视为不相等,因此我们得到了真实的结果。

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

console.log(a!=b);
console.log(a!==b);

Output

输出量

true
true

Both our objects are completely identical yet the != as well as the !== operator returns true indicating they're both unequal. Why is that so? This has something to do with the memory addressing of variables. Both the objects occupy completely different blocks of memory and are thus considered as two separate instances or two separate entities.

我们两个对象都是完全相同的,但是!=!==运算符返回true表示它们都不相等。 为什么呢? 这与变量的内存寻址有关。 这两个对象占用完全不同的内存块,因此被视为两个单独的实例或两个单独的实体。

let ob3 = ob1;

console.log(ob1 != ob3);
console.log(ob1 !== ob3);

Output

输出量

false
false

Since now ob3 occupies the same address as ob1, they're considered completely identical. Can you conclude which out of the != and !== does a more strict comparison than the other?

由于现在ob3与ob1占用相同的地址,因此它们被视为完全相同。 您是否可以得出结论,在!=!==中哪个比另一个更严格?

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

javascript运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值