Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it OK to use == in JavaScript?

From: http://www.2ality.com/2011/12/strict-equality-exemptions.html

JavaScript has two operators for determining whether two values are equal:

  • The strict equality operator === only considers values equal that have the same type.
  • The “normal” (or lenient) equality operator == tries to convert values of different types, before comparing like strict equality.

The advice given to JavaScript beginners is to completely forget about == and to always use ===.

But what is the reason behing it for not using == operator? Will it result to security risk?

But using typeof operator we can be sure that the result will be a string. Then == is safe to use, because we can be sure that it won’t perform any conversion shenanigans:

if (typeof x == "function") {
    ...
}
like image 585
Maizere Pathak.Nepal Avatar asked Apr 02 '13 16:04

Maizere Pathak.Nepal


3 Answers

The == operator is OK to use when the code is comfortable with implicit conversions taking place under the hood. This process, while counter intuitive at times, is well defined.

However I would still absolutely use === in the sample you provided. When no conversions are expected to take place then using == introduces confusion to the reader. It says "i expect conversions" when in fact no conversions could occur. You should be using the most specific operator which satisfies the condition you are looking for

if (typeof x === "function") { 
  ...
}
like image 173
JaredPar Avatar answered Oct 07 '22 19:10

JaredPar


If you're sure that it won't do any "conversion shenanigans", as per your example, then yes, you should still use ===, because you'll save the parser the effort of having to work out whether it needs to do a conversion. So you'll get better performance. (marginal, but still)

This isn't about security; it's about performance and about doing things the right way.

The only time you should use double equal is where you know in advance that you definitely do want to use javascript's implicit type conversion. This might be the case for example if you want to check the numeric value of an input field (which would be string). But even then, in a lot of cases you would be better to convert the value manually first.

In short, use the === syntax everywhere unless you are certain that you need the implicit conversion functionality of ==.

like image 43
Spudley Avatar answered Oct 07 '22 18:10

Spudley


When I originally read your question I thought of two scenarios:

  1. When comparing user inputted values to integers
  2. When comparing an object to null or undefined, because null does not explicitly equal undefined

But I did a little reading around and came across this, which is a pretty strong case for never using ==. I suppose it depends on your intentions, if you want a quick (and arguably nasty) catch-all then use ==, but its probably better practice (and a little extra work) to implement an explicit comparison.

like image 1
Crwydryn Avatar answered Oct 07 '22 18:10

Crwydryn