Why does the OR operator behave like this in Javascript? [duplicate] - javascript

This question already has answers here:
How does javascript logical assignment work?
(6 answers)
Javascript || operator
(5 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 3 years ago.
I was always using the || as a nullish coalescing sort of operator. Either A or B.
But if I do something like,
1 const a = null
2 const b = '123'
3
4 console.log(a || b) // 123
5 console.log(a) || console.log(b) // null, 123
Why are BOTH console logs executed on line 5? Even if a === null, shouldn't it just execute the first console log, and not look at console.log(b)?

The left side is evaluated.
console.log(a) returns undefined (because that function always returns undefined)
Since the LHS evaluated as a falsy value: The right side is evaluated.
The value of a is irrelevant as that isn't what is on the LHS.

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.

Strict equality works on variables but not on objects [duplicate]

This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Why is `Object() === new Object()` equal to `false`? [duplicate]
(1 answer)
Closed 3 years ago.
I have a variable defined as:
var o = new String("0");
In console when i write:
o === o
it returns true
but when i write:
new String("0") === new String("0")
it returns false
I don't understand why is it working on variable references but not on objects?
I tried it as:
(new String("0")) === (new String("0"))
because the problem may arise due to operator precedence, but it still returns false
new String("0") === new String("0")
Here you are comparing two different Strings having different references. Thats why you are getting false.
o === o
Here, You are actually comparing the same string (reference is same in this case).

How JS engine distinguish null from undefined [duplicate]

This question already has answers here:
What is the difference between null and undefined in JavaScript?
(38 answers)
JavaScript checking for null vs. undefined and difference between == and ===
(8 answers)
Closed 4 years ago.
In below code snippet a==b return true, i.e they point to same memory location, hence they will have same value.
I would like to know, how JS engine knows a===b is false.
How is type information determined when 2 different types point to same memory location?
Edit 1: From comments it looks like my question might not be clear. I totally understand difference between == and === in terms of usage in JS language. I am more interested in knowing how JS engine saves type information for null and undefined. As per my understanding variables a & b point to same memory location that is why I get a==b, if this understanding is wrong, please correct me.
Edit 2: Ok I will put my question in another way. How typeof operator knows a is object and b is undefined despite having a==b.
var a = null;
var b = undefined;
console.log(a==b);
console.log(a===b);
console.log(typeof a);
console.log(typeof b);
In the code snippet added in the question:
var a = null; var b = undefined;
console.log(a==b);
console.log(a===b);
console.log(a==b) returns true because == uses type coercion to check the equality of both the variables. Therefore, null and undefined are thought of as equals.
console.log(a===b) returns false because === does not use type coercion. For ===, null and undefined are not same types and it doesn't care about checking deep equality when the operands aren't of the same type.
This has got nothing to do with memory locations.
a = null, b= undefined;
a == b /* only check their values */
a === b /* also check their types + values */
typeof a == typeof b // false
typeof "variable" gives the type of the variable.

What is the purpose of || in a JavaScript variable? [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
How does javascript logical assignment work?
(6 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 4 years ago.
I just saw a line of code like this one below and I was intrigued by the use of ||
const myCars = ['BMW','Audi','VW']
const foo = myCars.length || [];
Does this mean if myCars.length was to ever be undefined that foo would equal an empty array instead of undefined?
Yes, how it should be read is:
if 'myCars' doesn't have a length (e.g. no values), the constant foo should be set to [].
Note: https://www.w3schools.com/jsref/jsref_length_array.asp, specifically the return value of the .length: "A Number, representing the number of elements in the array object".
this is Short-circuit evaluation in Javascript. its Unique in JS to USE || operator because other languages use this operator in conditional statements only. please read this
https://en.wikipedia.org/wiki/Short-circuit_evaluation

Why in Javascript is it possible for a boolean operator to return neither false nor true? [duplicate]

This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 7 years ago.
var operand1 = null;
var operand2 = true;
var booleanOperatorReturnsABoolean = operand1 && operand2;
booleanOperatorReturnsABoolean == false || booleanOperatorReturnsABoolean == true
Result: false
Running this in a javascript console shows that the boolean operator (&&) sometimes may NOT result in a boolean
Because that's how they're designed.
&& returns the left side of the expression if it's falsy, otherwise, it returns the right side.
|| returns the left side of the expression if it's truthy, otherwise, it returns the right side.
In cases where the left side is returned, the right side isn't even evaluated, allowing you to code shortcuts like:
var hasValue = "foobar" === someVariable;
hasValue && doSomething();
Here, doSomething will only be executed if somevariable equals "foobar"

Categories