By bryantsai

2010-01-25 14:24:51 8 Comments

As Which equals operator (== vs ===) should be used in JavaScript comparisons? indicates they are basically identical except '===' also ensures type equality and hence '==' might perform type conversion. In Douglas Crockford's JavaScript: The Good Parts, it is advised to always avoid '=='. However, I'm wondering what the original thought of designing two set of equality operators was.

Have you seen any situation that using '==' actually is more suitable than using '==='?


@Sarfraz 2010-01-25 14:28:20

Consider a situation when you compare numbers or strings:

if (4 === 4)
  // true


if (4 == "4")
  // true


if (4 === "4")
  // false

This applies to objects as well as arrays.

So in above cases, you have to make sensible choice whether to use == or ===

@bryantsai 2010-01-25 14:39:39

I'm not asking the difference between the two, which is answered in 359494. I'm interested to know when == would be especially suitable.

@Anonymous 2010-01-25 14:45:01

@bryantsai: He gave you that -- comparing strings to numbers without manually using parseInt.

@bryantsai 2010-01-25 14:50:07

mm.. didn't really spot that, thanks.

@Sarfraz 2010-01-25 16:45:05

i have said this in my answer, mostly it comes to numbers and strings and sometimes other data like objects and arrays. thanks

@Matthias 2010-01-25 16:45:11

The simple answer is that '==' makes more sense than '===' when you want type coercion to happen during comparisons.

A good example would be numbers being passed in a URL query string. If for instance you have paginated content, and the page query parameter holds the current page number, then you could check for the current page with if (page == 1) ... even though page is actually "1", not 1.

@Graza 2010-01-25 14:31:15

I would suggest that there is no problem with using ==, but to understand when and why to use it (i.e. use === as a rule, and == when it serves a purpose). Essentially, == just gives you shorthand notation - instead of doing something like

if (vble === 0 || vble === "" || vble === null || vble === undefined || vble === false) ...

It's much easier to just write

if (vble == false) ...

(Or even easier to write)

if (!vble) ...

Of course there are more examples than just looking for "truthy" or "falsey" values.

Really, you just need to understand when and why to use == and ===, I don't see any reason why not to use == where it fits better...

Another example is using this shorthand to allow shorthand method calls:

function func(boolOptionNotCommonlyUsed) {
  if (boolOptionNotCommonlyUsed) { //equiv to boolOptionNotCommonlyUsed == true
    //do something we rarely do
  //do whatever func usually does

func(); //we rarely use boolOptionNotCommonlyUsed, so allow calling without "false" as an arg

@Ravia 2010-01-25 14:27:41

== compares whether the value of the 2 sides are the same or not.

=== compares whether the value and datatype of the 2 sides are the same or not.

Say we have

$var = 0;

if($var == false){
  // true because 0 is also read as false

  // true because 0 is also read as false

if($var === false){
  // false because 0 is not the same datatype as false. (int vs bool)

if($var !== false){
  // true becuase 0 is not the same datatype as false. (int vs bool)

if($var === 0){
  // true, the value and datatype are the same.

you can check

@Gumbo 2010-01-25 14:32:54

-1 It’s about JavaScript and not PHP.

Related Questions

Sponsored Content

84 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

5 Answered Questions

[SOLVED] What is TypeScript and why would I use it in place of JavaScript?

  • 2012-10-02 16:37:58
  • Mohammed Thabet
  • 474728 View
  • 1573 Score
  • 5 Answer
  • Tags:   javascript typescript

42 Answered Questions

[SOLVED] When to use double or single quotes in JavaScript?

32 Answered Questions

[SOLVED] How to execute a JavaScript function when I have its name as a string

  • 2008-12-11 15:47:10
  • Kieron
  • 474443 View
  • 967 Score
  • 32 Answer
  • Tags:   javascript

10 Answered Questions

16 Answered Questions

[SOLVED] Why would a JavaScript variable start with a dollar sign?

4 Answered Questions

[SOLVED] When is it OK to use == in JavaScript?

14 Answered Questions

[SOLVED] Does it make sense to use Require.js with Angular.js?

2 Answered Questions

Sponsored Content