javascript, strange comparison operator [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
I've met "!==" strange comparison operator in source code of some chrome extension. Code snippet:
function closedTab(id) {
if (openedTabs[id] !== undefined) {
openedTabs[id].time = timeNow(0);
closedTabs.unshift(openedTabs[id]);
}
}
This operator is not used just once, so there's should some meaning.
Is "!==" came from some JavaScript magic? Or it's just equivalent to usual "!="?
Thanks

The difference is that !== doesn't try to convert its operands to the same type before comparing. Same with === and ==.
See this question: Difference between == and === in JavaScript

!== is the not identity comparison operator.
!= will coerce the two types to match
!== will not coerce the two types
For a few examples:
3 == "3" // true - the operands are coerced to the same type, then compared and they match
3 === "3" // false - the operands are not coerced to the same type, so do not match
3 != "3" // false
3 !== "3" // true

This is called a strict comparison operator where it not only checks for value but also for type.

The difference comes from what happens when values are of different type.
The !== operator (and its cousin ===) checks on the equality of both value and type. != and == on the other hand try to coerce values to be the same before checking equality.
For example:
if(5 === "5") // evaluates to false
if(5 == "5") // evaluates to true
The same concept is extended to != and !==

It's an identical comparison operator. It compares value and type. For example:
if('foobar' == true) // true
if('foobar' === true) // false

Related

JavaScript checking for nullish value as part of an if block? [duplicate]

This question already has answers here:
How can I determine if a variable is 'undefined' or 'null'?
(34 answers)
Closed last month.
Is it possible to check for nullish values as part of an if block, without resorting to a pair of explicit checks for null and undefined.
For example, don't want to do this:
if(key === null || key === undefined) {
// do something
}
This doesn't work, because it also includes other falsy values such as zero and empty array.
if(!key)
That's one of the good use cases of the operator ==:
if(key == null) {
// do something
}
This will work with both null and undefined, while keeping away other falsy values.

Understanding JavaScript evaluation [duplicate]

This question already has answers here:
Operator precedence with JavaScript's ternary operator
(7 answers)
Closed 3 years ago.
I am fixing some code and trying to understand a certain evaluation that is happening. There was a little snippet written where 2 evaluations should happen like so :
const active = true;
const isPerson = true;
const person1Time = null;
const person2Time = null;
// this one is not working as intended
const original = !active && isPerson ? !person1Time : !person2Time;
console.log("original", original);
// fixed when second evaluation is wrapped in parens
const fix = !active && (isPerson ? !person1Time : !person2Time);
console.log("fixed", fix);
I was able to fix the code to the desired result by wrapping the ternary evaluation in parentheses. What I am wondering is - why it works like this? The !active evaluates to false, and the ternary evaluates to true, and a console.log(true && false), this evaluates to false. Perhaps I am misunderstanding something here.
Boolean operators take precedence over ternary operators. In other words, this:
!active && isPerson ? !person1Time : !person2Time;
is equivalent to this:
(!active && isPerson) ? !person1Time : !person2Time;
Therefore, you obtain a different result.
Follow the operator precedence rules:
Binary AND (x && y) is priority 6.
Ternary (x ? y : z) is priority 4.
Unary NOT (!x) is priority 17.
Therfore, the NOT operator is evaluated first, then the binary AND, and lastly the ternary operator.
The original is evaluating the entire conditional expression on the left side of the ternary operator: !active && isPerson. Then based on the result of that expression - true or false - it evaluates the correct result.

Precedence If Clause Parenthesis and Equality [duplicate]

This question already has answers here:
Which Logic Operator Takes Precedence
(6 answers)
The order of operators in JavaScript (|| ,&&)
(4 answers)
Closed 3 years ago.
Parenthesis have higher precedence than equality.
So from my understanding, the higher precedence is evaluated first.
Lets now assume I compare a which is not defined:
if (a == 1) { .. // throws an exception because a is not defined
Actual case:
if (typeof a !== 'undefined' && (a == 1)) {
console.log(1)
} else {
console.log(2)
}
should be evaluated in this order:
(typeof a !== 'undefined' && (a == 1))
(a == 1)
typeof a
result from (3) !== 'undefined'
"result from (4)" && "result from (2)"
But this would usually throw an exception if a is not defined but it doesn't.
Why is the left side of the equation evaluated first even though it has the lower precedence?
EDIT: I adapted the example from '||' to && just because || would always throw.
|| is evaluated left-to-right:
5 Logical OR left-to-right … || …
So the inner expression that gets evaluated first is typeof a, as part of typeof a !== 'undefined'.
If the right-hand side of the || contains more expressions nested in parentheses, regardless of their operator precedence, they will only be evaluated after the left-hand side of the || has been evaluated.

There's difference between "$var === true" and "true === $var"? [duplicate]

This question already has answers here:
What is a reason of placing boolean or null before comparison operator?
(3 answers)
Closed 8 years ago.
A few time ago, I saw in a JavaScript application (I don't remember which one is) the use of true === $var instead of $var === true. Example:
if (true === $var) {
// do something
}
I think these comparisons are differnet but I can see why — someone can explain it for me? And plus: when I should use and when I shouldn't.
Duplicated?
I searched on the web but no results.
There is no difference between them.
Yoda comparisons are done by some people to prevent a typo (missing =s) creating an assignment instead of a comparison (since true = $var will throw an error while $var = true will not).
I think this is just a best practice kind of thing, at least I'm used to seeing the suggestion when coding in languages such as C#, where it's easy to accidentally do
if(someVar = 1)
instead of:
if(someVar == 1)
Doing it in reverse prevents that possibility, as the compiler will pick up the fact that you can't assign the value of a variable to a literal (or constant, if you use constants).
No there is not any difference in both
But there is difference in $var == false and $var === false
$var = 0;
$var == false // TRUE
$var === false // FALSE
=== will also compare the variable type. It checks if it's a bool, string or integer for example.
$var = "1";
$var == 1 // TRUE
$var === 1 // FALSE

When should you use === vs ==, !== vs !=, etc.. in javascript? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
What are the differences between === vs == and !== vs !=?
When should you use each one?
=== is the Identity operator, and is used to test that value and type are equal.
so..
"3" == 3 // true
"3" === 3 // false
1 == true // true
1 === true // false
"1" == true // true
"1" === true // false
so when you care that value and type are equal, or not equal use Identity operators === or !==
The "normal" == operators in javascript perform type coercion, and try their best to do things like treat a string as number or an object as a string where required. The longer === operators will not do type coercion, but rather a strict comparison within the type.
=== and !== are the same as == and !=, but additionally do checks for the variable types.

Categories

Resources