Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  156] [ 6]  / answers: 1 / hits: 75406  / 14 Years ago, fri, september 17, 2010, 12:00:00

Recently I was running some of my code through JSLint when I came up with this error. The thing I think is funny about this error though is that it automatically assumes that all == should be ===.



Does that really make any sense? I could see a lot of instances that you would not want to compare type, and I am worried that this could actually cause problems.



The word Expected would imply that this should be done EVERY time.....That is what does not make sense to me.


More From » jslint

 Answers
9

IMO, blindly using ===, without trying to understand how type conversion works doesn't make much sense.



The primary fear about the Equals operator == is that the comparison rules depending on the types compared can make the operator non-transitive, for example, if:



A == B AND
B == C


Doesn't really guarantees that:



A == C


For example:



'0' == 0;   // true
0 == ''; // true
'0' == ''; // false


The Strict Equals operator === is not really necessary when you compare values of the same type, the most common example:



if (typeof foo == function) {
//..
}


We compare the result of the typeof operator, which is always a string, with a string literal...



Or when you know the type coercion rules, for example, check if something is null or undefinedsomething:



if (foo == null) {
// foo is null or undefined
}

// Vs. the following non-sense version:

if (foo === null || typeof foo === undefined) {
// foo is null or undefined
}

[#95585] Wednesday, September 15, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;