Skip to content Skip to sidebar Skip to footer

== Vs. === In A Remote JavaScript File. Which One Is Faster?

Possible Duplicate: JavaScript === vs == : Does it matter which “equal” operator I use? When comparing two operands of the same type for equality in JavaScript, using == or

Solution 1:

As you say, for comparison where both operands are guaranteed to be of the same type, the two operators are specified to perform precisely the same steps and, barring compiler optimizations, are likely to perform near enough identically. Therefore there is a slight advantage in terms of file size in using == over === in those cases.

However, some people argue that consistency is more important: === is usually closer to what you intend when testing equality, and only using === and !== is something many people find helpful and readable. Personally, I have the opposite rule and only use === when there is uncertainty about the types of the operands, but I wouldn't recommend either way over the other.

If you understand the differences between strict and non-strict equality and you're confident that using == and != won't cause you or anyone else working your code any problems reading and understanding code in the future, go ahead and use them.


Solution 2:

Presumably blogs that recommend one over the other are doing so for reasons of logical consistency with analogous operations and not for speed. Trying to trim your Javascript programs to point of shaving off individual characters from the script is unwise; running it through minify or another automated tool before serving is one thing, but hand-tuning Javascript for minimum file size or for execution speed on the level of individual operands is an endless, thankless task that will make your site harder to maintain.

Use whichever operand makes more logical sense to you, so you won't be confused when you don't remember this line of inquiry two years from now.


Solution 3:

If I may quote from Douglas Crockford's Javascript: The Good Parts:

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. The rules by which they do that are complicated and unmemorable ... The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==.

Whether there is a performance difference or not, it would be difficult to justify the use of the "evil twins".


Solution 4:

var test="1";
console.log(test==1); // true
console.log(test===1); // false

== checks whether test has 1 or not but === checks whether test has 1 and also checks it's data type. In that case 2nd expression is false because it's data type is string (test is string) but the right hand operand is not string. The following test is different

var test=1;
console.log(test==1); // true
console.log(test===1);​ // true

Because test contains Integer 1 that evaluates to Boolean true and right hand operand is also same. === also checks whether both operands type.


Post a Comment for "== Vs. === In A Remote JavaScript File. Which One Is Faster?"