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

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.

Related

how to check that a variable contains something in JavaScript [duplicate]

This question already has answers here:
if else statement reverse
(4 answers)
Closed 12 days ago.
Good morning
there are many tutorials that explain how to check if a string is empty but not the other way around
so i would like to do this
if variable not empty then we do this
if not empty then we do this
//do the opposite
if (nomvalidation === null || nomvalidation.trim() === ""){
}
Simply change the equality operators to inequality and the or to and and.
if (nomvalidation !== null && nomvalidation.trim() !== ""){
}
You may also want to look into the typeof operator to make sure its a string and undefined as another possible "empty" state.

Can I use double equal in JavaScript if one of the elements is a string? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm newbie in JS, I know that == double comparation for equality and === triple one for equality and type. My teacher says that I should use always triple. However, I don't see any down effect if I use double equal when one of the elements is a string.
'' == 0 // true
'10' == 10 // true
'dog' == 'cat' // false
So, I'm using always double equal when some of the element is a string (avoiding this 'false' == false) and triple when I'm detecting null or undefined.
Is there any down side for my approach?
I checked this previous answer: Which equals operator (== vs ===) should be used in JavaScript comparisons?
Using my personal working experience as a basis, I have never user the abstract equality operator(==) over the strict equality operator(===).
Since the == operator may have some unpredictable behavior, like
'0' == false // true
While
'0' === false // false
The second case is normally what you want your code to do.
Also, the == operator is more performance-costly than the === operator.
You can read more about this on the ecmaScript documentation:
The Equals(==) Operator: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.1
The Abstract Equality Comparison Algorithm: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
The Strict Equals(===) Operator: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.4
The Strict Equality Comparison Algorithm: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6 (these two comparison algorithm probably will clarify to you why the strict equality is better on performance)
If you read the linked answer, you also read this chunk of code involving strings:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
Those are string, but still the behaviour is not exactly what you may expect. Indeed using === can make your life easier as your teacher said :)
If you use '10' == 10 , then the js interpreter must do a type conversion. This costs a little performance.
If you are using code quality tools like sonarqube, you will get some 'hints' to fix it.
It depends on the ocassion.
For example if you try to check if 9 == '9' it would return true, but they are in fact different, because one of then is a word(string) and the other is number(integer). In some of the cases that would make you suffer later.
The best scenario is to know what you are expecting it to be - is it a string, integer or something else and use the === or atleast check the type of the variable.

If [] is falsy, how come ![] is also falsy [duplicate]

This question already has answers here:
Empty arrays seem to equal true and false at the same time
(10 answers)
Closed 3 years ago.
We know that in JS [] == false returns true. But today I was watching a video presentation by Kyle Simpson and I saw this:
[] == ![] // true
How come ![] is also falsy. As per my logic, it should be true.
== allows coercion. So does ! operator. So [] == ![] becomes "" == false which is true. Further ![] is true because [] is an object which is a true value. And negation of that yields false.
In Javascript, not operator (!) check for not condition like this !(true), here first it checks the inner part of (), If it returns true then the actual output becomes false.
(true) => !(true) =>false
(false)=>!(false)=>true`
Get back to original question
[] return true where as ![] return false when you compare strictly with === its check for type and value both.
console.log([]); // true
console.log(![]); // false
console.log([] == ![]) // true
console.log([] ===![]) // false
Note if you want boolean value all the time then you can use !!.
console.log(!![]); // true
console.log(!!(1 === 1)); // true

What is the difference between != null and !== null? [duplicate]

This question already has answers here:
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 6 years ago.
Sorry for what I'm sure had have been asked in the past, but it's very hard to search for a question like this. "!=" and "!==" are not exactly search friendly. If anyone knows a duplicate question you can point me to it.
What is the difference between doing myVar != null and myVar !== null?
I know that != is not equal and !== is not equal value or not equal type, but when comparing to null is there ever a case where they would return different results? Is one better to use than the other?
The answer to the specific question about whether there's ever a case where != and !== comparisons involving null get different answers is yes:
undefined != null // false
undefined == null // true
undefined === null // false
undefined !== null // true
The rules for == and != explicitly include a clause that stipulates that null and undefined are the same.
Personally — that is, in my code — that fact is a reason for using != (or ==) when checking for null in cases where undefined should be treated the same way (which is a pretty common situation).

javascript, strange comparison operator [duplicate]

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

Categories

Resources