This question already has answers here:
Closed 11 years ago.
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!
=== 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.
Related
This question already has answers here:
Check variable equality against a list of values
(16 answers)
How to shorten my conditional statements
(15 answers)
Closed 3 years ago.
For example, instead of:
if (ext === "_st" || ext ==="_mt" || ext === "_00" ){
//do something
}
I would intuitively expect that the following also works:
if (ext === ("_st" || "_mt" || "_00" )){
//do something
}
But that doesn't work.
Is there a way to avoid repeating the same ("ext" in this case) variable for more compressed and efficient code?
UPDATE: I did a search before asking and none of the "duplicate" questions appeared in the first 10-15 suggestions. Instead, StackOverflow made WRONG suggestions for irrelevant -false duplicates.
Here is my suggestion:
Instead of marking questions as "duplicate" why not merge them along with the answers in one single question with an intuitive title?
Thanks for the answers.
UPDATE #2: This is NOT a duplicate. The suggested questions have answers that only refer to OR optimization while my question is more general, it refers to "logic expressions", NOT specifically to OR. I just provided an OR example. I'm specifically asking whether there is a method to avoid repeating the main variable in a logical statement whether it is OR, AND, etc.
For example: given var1 == var2 == var3
if (ext === var1 && ext === var2 && ext === var3 ){
//do something
}
var ext = "_00";
if (/^_st$|^_mt$|^_00$/.test(ext) ){
console.log("do something");
//do something
}
Using regex this is possible.
Explanation:
Each comparison is separated by an |(or) and enclosed in ^ and $ to match exactly otherwise it would match else where.
let ext = "_mt";
if( ext.match( /_st|_mt|_00/g ) ) {
console.log("Yes : type 1");
}
if( ["_st", "_mt", "_00"].includes( ext ) ) {
console.log("Yes : type 2");
}
As #Shily said you can use an array and this kind of regex too
This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
What's the reason to use === instead of == with typeof in Javascript?
(5 answers)
Closed 6 years ago.
Say I wanted a function that checked whether or not a variable is a string, the interwebs advised me as follows:
function is_string(s) { return typeof s === 'string'; }
but I can't think of any scenario where "typeof s" could return "string" without s actually being a string.
Is there any reason for that === operator instead of ==?
Reason being, I want to check types in a switch statement, AFAIK, the switch statement uses the loose comparison operator. Since I'm only looking for known types and don't need to check for undefined, it should be fine, right?
This question already has answers here:
JavaScript checking for null vs. undefined and difference between == and ===
(8 answers)
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 7 years ago.
Very often we check our objects if they are not null and not undefined. I always use condition if (obj !== null && obj !== undefined). Few days ago my colleague shown me the syntax of double inversion !! and now I can use condition if (!!obj). This syntax is less.
I'm not a person who are only learning js, but I have a little interest.
So is there any difference between these two ways of object validation? Performance difference? Semantic difference? Any difference?
There isn’t any particularly good reason to ever use if (!!obj), as it’s equivalent to if (obj). !!obj and obj !== null && obj !== undefined do different things, though, and you should use whichever’s most appropriate.
obj !== null && obj !== undefined (surprise, surprise) results in false for null and undefined.
!!obj results in false for anything falsy, including null, undefined, '', 0, NaN, and false.
!!foo is used to force coerce a value into its Boolean value. This is, in my experience, often used in APIs that want to return a Boolean value for a value that contains sensitive data. For example, to return whether or not a password is entered you might say return !!password rather than return password.
Continuing on that, if (!!obj) is not the same as if (obj !== null && obj !== undefined) for many values you can think of! Such as false.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
Javascript operator !==
What's the difference between != and !==?
Can you give me an example where using != gives another result than using !==?
alert(1 != true);
alert(1 !== true);
The first one is false, the second true.
!= accept 1 as equals of true, null as equals of false and some others (because the values are automatically casted when being compared).
!== accept only "real" equalities (i.e. compares both the value and the type).
Example
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Why do I see lots of javascript code lately with expressions that look like this:
if(val === "something")
Why "===" instead of just "=="? What's the difference? When should I use one or the other?
The === does not allow type coercion, so something like this would return false:
if (2 === '2') // false
The "normal" javascript == operator does allow type coercion, and so this would return true:
if (2 == '2') // true
var a = 3;
var b = "3";
if (a == b) {
// this is true.
}
if (a === b) {
// this is false.
}
=== is typically referred to as the identity operator. Values being compared must be of the same type and value to be considered equal. == is typically referred to as the equality operator and performs type coercion to check equality.
An example
1 == '1' // returns true even though one is a number, the other a string
1 === '1' // returns false. different datatypes
Doug Crockford touches briefly on this in JavaScript the Good Parts google tech talk video. Worth spending an hour to watch.
Checks that the type as well as the values match. This is important since (0 == false) is true but (0 === false) is not true.