2

Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?

Hi, This is my tiny code:

var domains_before_update = storage.getItem('domain_list_original');
    if(domains_before_update==null || domains_before_update=="" )
    { 
           gBrowser.selectedTab = gBrowser.addTab("chrome://filter/content/block_and_redirect_list.html");

    }

Is that correct or should I be using === instead of == ?

Thanks!

Community
  • 1
  • 1
Ryan
  • 9,821
  • 22
  • 66
  • 101
  • 2
    Hint: Triple equal operator is what you want there. See here: [Javascript === vs == : Does it matter which "equal" operator I use?](http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use) – Wesley Murch Jun 02 '11 at 19:50
  • Depends on if Javascript's type-casting rules would guess wrong. What possible types can storage.getItem return? – Marc B Jun 02 '11 at 19:51

4 Answers4

2

=== checks the strict equals (without coercion) that you're used to , where == checks the value [after built-in coercion] equality

but as the other answer(s) noted, strict equality does not work when checking for null, use !variable

same as this post: Difference between == and === in JavaScript

edit: clarified some of the wording thanks to the helpful comments!

Community
  • 1
  • 1
Brett
  • 4,051
  • 2
  • 26
  • 39
  • @brett, 2 == "2" is true, so are you sure it checks type equality? – hvgotcodes Jun 02 '11 at 19:52
  • @hvgotcodes - I think the word from the other post 'type-converting' equality is probably the best word... – Brett Jun 02 '11 at 19:54
  • @brett, right i think you just chose the wrong word. But 'type equality' is definitely wrong. – hvgotcodes Jun 02 '11 at 19:57
  • @brett Should be *value [after built-in coercion] equality* :-) `===` *effectively* suppresses the coercion (okay, okay, the specification defines it a little different than "suppresses the coercion" but the result is the same). –  Jun 02 '11 at 20:03
  • @pst - thanks for the help! assuming you don't mind, I used your comment in the post to clarify things a bit. – Brett Jun 02 '11 at 20:07
  • @Brett Lol. I don't mind. I'd up-vote, but I'm so vote-promiscuous I'm out. –  Jun 02 '11 at 20:21
1

In this case, it doesn't matter - and in all cases where it doesn't matter, you should use strict equality or identity, e.g. ===.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • So that would be if(domains_before_update===null || domains_before_update==="" ) Yes? – Ryan Jun 02 '11 at 20:45
  • @Ryan, @minitech: Actually it does matter in this case: if `domains_before_update` is 0 you will get `true` for `==` and `false` for `===`. In cases where it really doesn't matter, make your own decision, considering things like readability (will you have to stop and think if you look at your code later and see `==` rather than `===` or vice versa? If so, is that a good or a bad thing?). Personally, I use `==` when it doesn't matter, but I've been through phases of using `===` or a mixture. – Tim Down Jun 02 '11 at 23:30
1

Neither.

Use:

if(!domains_before_update)
{

}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    Not strictly, it depends on the possible return values of `getItem()` and how he wants to handle them. However, in general this will be true. – Wesley Murch Jun 02 '11 at 19:53
  • 1
    @Wesley Murch - Yes, I am assuming he wants to check for falsy values. – Oded Jun 02 '11 at 19:54
  • getitem is returning a string (a large collection of domains), I just want to see if it is empty...if it returns anything other than empty or null then I am not interested in it. – Ryan Jun 02 '11 at 20:45
0

For comparisons with null, === is required.

ic3b3rg
  • 14,629
  • 4
  • 30
  • 53