Javascript: == or ===? [duplicate]
Possible Duplicate:
J开发者_StackOverflow社区avascript === 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!
===
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!
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. ===
.
Neither.
Use:
if(!domains_before_update)
{
}
For comparisons with null, === is required.
精彩评论