Checking for `null` in JavaScript [duplicate] - javascript

This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
Javascript comparison question (null >= 0) [duplicate]
(4 answers)
Closed 2 years ago.
I am a little confused by this:
0 > null // gives me `false`
0 === null // gives me `false` - also
0 == null // is `false`
How come 0 >= null becomes true?

Comparisons convert null to a number, treating it as 0. That’s why 0 >= null is true.
Example:
0 === Number(null) // true
0 >= Number(null) // true
Edit:
There's a difference on how null is handled in comparisons like >=, <=, >, < and how it is handled in equality checks like ===, == .

Related

null comparison with 0 not as expected as it showing the result with javascript [duplicate]

This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
Closed 8 months ago.
can someone explain the null comparison with 0
null > 0 // false
null == 0 // false
null > 0 || null == 0 // false
null >= 0 // true
why it happening ?
The reason is that an equality check == and comparisons > < >= <= work differently. Comparisons convert null to a number, treating it as 0. That’s why null >= 0 is true and null > 0 is false.
On the other hand, the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don’t equal anything else. That’s why null == 0 is false.
Source: Comparisons
In Javascript null isn't equal to 0 but equal to undefined.
null == undefined // true
Also :
Strange result: null vs 0
Let’s compare null with a zero:
alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true
Mathematically, that’s strange. The
last result states that "null is greater than or equal to zero", so in
one of the comparisons above it must be true, but they are both false.
The reason is that an equality check == and comparisons > < >= <= work
differently. Comparisons convert null to a number, treating it as 0.
That’s why (3) null >= 0 is true and (1) null > 0 is false.
On the other hand, the equality check == for undefined and null is
defined such that, without any conversions, they equal each other and
don’t equal anything else. That’s why (2) null == 0 is false.
Comparisons

why the value of console.log(1 && 2) is 2 instead of true or 1 in javascript? [duplicate]

This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 4 years ago.
I had checked for many test cases. It's looking if the first value is true and it's returning the second value.
Example:
2 && 5 // --> 5
5 && 10 // --> 10
0 && 2 // --> 0
Why doesn't it return either True or 1?
Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Description,
Operator: Logical AND (&&)
Usage: expr1 && expr2
Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
The && operator will return the last value if all other values are truthy, otherwise it will return the first non truthy value. So, as in 0 && 2, 0 is non-truthy, that gets returned.

Why does this function return 0 [duplicate]

This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
All falsey values in JavaScript
(4 answers)
Closed 5 years ago.
I am trying to figure out why this javascript function returns 0 when this.position.startOffset value is 0 but not when the value is a number other than 0.
ready: function() {
return (this.position.startOffset
&& this.position.endOffset
&& this.position.representation.trim().length >= 0
&& this.text.id
&& this.user.id
&& this.concept);
}
The && chain will stop evaluating at the first non-truthy (falsy) value and return it. Since 0 is falsy it is returned when it is encountered. If no falsy value is encountered then the last value is returned:
var a = 55 && [] && 0 && false && true; // yeild 0 as it is the first falsy value to encounter
console.log("a:", a);
var b = 30 && true && [] && "haha"; // yeild "haha" as it is the last value (no falsy value encountered)
console.log("b:", b);
Falsy values are:
null
undefined
the empty string '' or ""
the number 0
boolean false
NaN
This happens due to how javascript handles numbers in boolean expressions.
If the first parameter in a and (&&) statement returns 0, then the right parameters will not be evaluated and 0 will be returned.

Behaviour of and operator in javascript [duplicate]

This question already has answers here:
Javascript AND operator within assignment
(7 answers)
Closed 7 years ago.
I am checking for the values of AND operator in javascript, the below code for some reason returning 0. Can someone explain actual behavior of AND operator here?
var result = 88 && 6 && 0 && null && 9;
alert(result);
&& evaluates as the left hand side if the left hand side is false, otherwise it evaluates as the right hand side.
(88) && (6 && 0 && null && 9)
88 is true, so the right hand side is evaluated.
(6) && (0 && null && 9)
6 is true, so the right hand side is evaluated.
(0) && (null && 9)
0 is false, so it is the result.

In javascript, 5 || 0 and 0 || 5 returns 5 [duplicate]

This question already has answers here:
What does the || operator do?
(4 answers)
Proper use of ||
(3 answers)
Javascript-like || in php [closed]
(1 answer)
Closed 8 years ago.
As the title says everything. 5 || 0 and 0 || 5 returns 5 in JavaScript. Why does this happen and what does two || means in javascript?
It's a boolean or, and 5 evaluates to truthy. If you want to force your types to boolean you should use the !! (double negation) like so,
!!(5 || 0)
|| is a boolean or.
5 == true
0 == false
So, 5 || 0 = 5
The || is a synonym for the logical OR
So the statement ANY_VALUE || ANY_OTHER_VALUE means that if the first value is truthy then return that else return the second value

Categories