Javascript multiple OR in statement [duplicate] - javascript

This question already has answers here:
How do I test if a variable does not equal either of two values?
(8 answers)
Closed 2 years ago.
I have a very basic statement that isnt outputting the correct value but I cant get my head around why.
My variable has the following value:
var bedroomNums = "0_1"
Yet this output displays as true in Chrome console:
bedroomNums != ("0_0" || "0_1" || "0_2")
I am trying to create an IF statement to say:
if bedroomNums doesnt equal X or Y or Z then the statement is true
I have tried:
bedroomNums != "0_0" || bedroomNums != "0_1" || bedroomNums != "0_2"
but this also displays true
Where am I going wrong

What you're actually looking for is a logical AND, which would ensure that every condition is met:
if (bedroomNums != "0_0" && bedroomNums != "0_1" && bedroomNums != "0_2") {
// `bedroomNums` is not one of "0_0", "0_1", "0_2"
}
If one of these checks returns FALSE (that is, bedroomNums is set to one of these values), the code inside the curly braces will not be executed.
An alternative way to express this same condition would be to enumerate each of these values you're comparing against into an Array structure, then checking to ensure that the value of bedroomNums isn't present in said Array using Array.prototype.includes():
if (!["0_0", "0_1", "0_2"].includes(bedroomNums)) {
// `bedroomNums` is not one of "0_0", "0_1", "0_2"
}
This style is generally more helpful in situations where your program has to enumerate all possible values for a condition dynamically (based on some other logic or data source).

Related

.includes() on an empty string logic [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 2 years ago.
I'm trying to debug someone`s code. The "e" variable line is checking whether the user has "save-data" enabled or if he's on a slow (2g) connection.
e = navigator.connection && (navigator.connection.saveData || (navigator.connection.effectiveType || "").includes("2g"));
if (!e && d)...
The part I'm confused about is the empty brackets in the last OR statement. Is it just a redundant code or am I missing something?
What's the point for checking whether an empty string has "2g" in it?
(navigator.connection.effectiveType || "") will resolve to an empty string if effectiveType is null, undefined or some other falsy non-string value.
If you didn't do that you'd attempt to call null.includes() which would throw an exception.

Object is not null and not undefined. What is better - double inversion `!!` or strict equillity `!==` in JavaScript [duplicate]

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.

How can jquery || "OR" be used outside of an if statement? [duplicate]

This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
What does the construct x = x || y mean?
(12 answers)
In Javascript, what does it mean when there is a logical operator in a variable declaration? [duplicate]
Closed 8 years ago.
I've never seen the OR paramater || used outside of an if statement.
What does this line of code do?
var doc = ($iframe[0].contentWindow || $iframe[0].contentDocument).document
Is it saying making it equal to either one of those???
A || B
evaluates A first. If it is true, A is returned, and B never needs to be looked at.
If A is false, B is evaluated and returned.
For example, if you write
function (x)
{ x = x || 50
...
This would make x=50, if x is nil (or some kind of false value).
Otherwise, x would not be changed.
It is like having a default value, or a failsafe protection. If you know that the answer should never be false, then if A is false, you provide a backup value of B.
A way to get a DOM reference to the iframe's window object is to use:
contentWindow.document
Now, cause IE<8 has problems with it, a small polyfill is to use
var doc = ($iframe[0].contentWindow || $iframe[0].contentDocument).document;
// Browser you get this one ^^^ ? NO? Sh** you're IE7, go with^^
So earlyer versions of IE will skip the contentWindow cause not recognized, and thanks to the || (or) operator will follow up with the next contentDocument.
I don't have to repeat what's the OR operator cause other smart people already explained it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
This is just a short form of the ternary operator, which always returns a value depending to a statement. So, e. g.:
var fruit = "apple";
var test = fruit === "apple" ? fruit : "banana";
This sets the variable test to the value of fruit, when fruit is set to "apple". Otherwise, test will be initialized with "banana".

Why Constant first in an if condition [duplicate]

This question already has answers here:
Order in conditional statements [duplicate]
(2 answers)
Closed 9 years ago.
I often see if structures being coded like this:
if (true == a)
if (false == a)
Why do they put the constant value first and not the variable? as in this example:
if (a == true)
if (b == true)
This is called yoda syntax or yoda conditions.
It is used to help prevent accidental assignments.
If you forget an equals sign it will fail
if(false = $a) fails to compile
if($a = true) assigns the value of true to the variable $a and evaluates as true
The Wordpress Coding Standards mention this specifically:
if ( true == $the_force ) {
$victorious = you_will( $be );
}
When doing logical comparisons, always put the variable on the right
side, constants or literals on the left.
In the above example, if you omit an equals sign (admit it, it happens
even to the most seasoned of us), you’ll get a parse error, because
you can’t assign to a constant like true. If the statement were the
other way around ( $the_force = true ), the assignment would be
perfectly valid, returning 1, causing the if statement to evaluate to
true, and you could be chasing that bug for a while.
A little bizarre, it is, to read. Get used to it, you will.
This is an example of YODA Style of coding
if (var a == true){
}
is less safer than
if (true == var a){
}
because when you forget that second = mark, you'll get an invalid assignment error, and can catch it at compile time.

Difference between !== and != [duplicate]

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

Categories