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

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.

Related

Checking value with JavaScript [duplicate]

This question already has answers here:
How does the single equal sign work in the if statement in javascript
(6 answers)
Closed 3 years ago.
This is my general code (in JavaScript):
let x = Math.floor(Math.random()×6)+1);
if (x=1){
do this
} else if (x=2){
do that
} else if.... and so on.
Whenever I test this code in a browser, only actions that could happen in the {do this} section happen. x is being locked as 1, even though it is meant to be a fair 6 sided die. Any ideas why this is happening, and how I can fix it?
The = operator in JavaScript is for assignment. x=1 will always return true because that is an assignment that will never fail. To test for equality use == (equality with conversion) or === (strict equality).
You have your if (x=1) assigning the value of 1 to x, rather than checking if it's equal to it. This results in it always returning TRUE. Using a pair of == (or ===) will solve this.

Javascript multiple OR in statement [duplicate]

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).

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".

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