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運算符