Javascript OR conditional operator not working as expected [duplicate] - javascript

This question already has answers here:
How do I test if a variable does not equal either of two values?
(8 answers)
What's the prettiest way to compare one value against multiple values? [duplicate]
(8 answers)
Closed 2 years ago.
I am experimenting with JavaScript conditions. In the code below, why is my first condition not returning true while the second one does?
Javascript:
arr = [1,2,3];
if (arr[0] !== (1 || 2)){console.log('true')}
//undefined
if (arr[0] !== (2 || 1)){console.log('true')}
//true

|| will evaluate to either:
The first value, if it's truthy, or
The final value (which may be either truthy or falsey)
Since 1 is truthy, (1 || 2) evaluates to 1:
if (arr[0] !== (1 || 2)){console.log('true')}
// resolves to
if (1 !== (1 || 2)){console.log('true')} // resolves to
if (1 !== (1)){console.log('true')} // resolves to
if (1 !== 1){console.log('true')} // resolves to
if (false){console.log('true')}
So the first if statement does not go through. In the second case, 2 || 1 evaluates to the first truthy value of 2, so the if statement succeeds.

For this, you need an array of numbers to check against and negate the check as well.
const arr = [1, 2, 3];
if (![1, 2].includes(arr[0])) {
console.log(arr[0], 'is not', 1, 'or', 2);
}
if (![1, 2].includes(arr[2])) {
console.log(arr[2], 'is not', 1, 'or', 2);
}

Related

What does "value != value" mean in JS? [duplicate]

This question already has answers here:
How do you test for NaN in JavaScript?
(3 answers)
Can (a== 1 && a ==2 && a==3) ever evaluate to true?
(29 answers)
Closed 3 months ago.
I was reading the source code of core-js and I saw the following:
if (value != value) return true;
What does it actually mean? When exactly value won't be equal to itself?
It could be if:
value is NaN
console.log(NaN != NaN);
value is actually a getter on the global object, and it returns something different each time:
let i = 0;
Object.defineProperty(window, 'value', { get: () => i++ });
console.log(value != value);

How can I input a conditional statement in ternary operator to return true or false in React? [duplicate]

This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 6 months ago.
I have the values ​​lightsstasut1error, lightsstasut2error, lightsstasut3error and the strips contain either "Y" or "N". At this time, I want to return true if at least one of the values ​​is N, and false if all of them are Y. So I wrote the code, and if there is even one N, it keeps returning false. How do I fix my code?
this is my code
const errors2 = (lightstasut1error || lightstasut2error || lightstasut3error) === "N" ? true : false;
CHange this to this
const errors2 = (lightstasut1error === "N" || lightstasut2error === "N" || lightstasut3error === "N")
Please check now
Hope it helps, feel free for doubts

Why includes() behaves this way in js?

a=[1,2,3,4]
console.log(a.includes(4 || 5); //this yields true
console.log(a.includes(5 || 4); //this yields false
Why does the function includes() behave in this particular way?
includes only takes one value. You may think you're telling it to check for two things, but you're not. The || operator is going to compare the two operands and resolve to a single value. If the left hand side of the || is "truthy" then that will be used.
In short: 4 || 5 is no different from just 4, and 5 || 4 is no different from just 5. So your first line checks if a includes 4, and the second checks if it includes 5.
If you need to check multiple values, either call includes multiple times, or use a different method like .find
const a = [1,2,3,4];
const element = a.find(val => val === 4 || val === 5);
const found = !!element; // turning it into true/false instead of 4/5/undefined
console.log(found);
a=[1,2,3,4]
console.log(a.includes(4 || 5)); //this yields true
4 || 5 will be executed first. The Result is 4
4 is included in a and the Result is true
console.log(a.includes(5 || 4)); //this yields false
5 || 4 results in 5
5 is not included in a and the Result is false
The function includes() behaves as it should.
I guess the confusion comes from this part:
4 || 5 = 4
5 || 4 = 5
For Example: X || Y
If X is undefined, the result will be Y
If X is defined the result will be X
In both of your examples the "X" is defined with a Number. The result is as expected the fist Number.
I hope that was helpful :)

why is [ ]==0 in java script? [duplicate]

This question already has answers here:
Javascript array equal to zero but not itself
(4 answers)
Closed 4 years ago.
Code:
a=[1,2];
b=[];
if(b==0){
console.log('0')
}
if(a==2){
console.log('2')
}
if([]==0){
console.log('3')
}
Output:
0
3
in case if [ ] is considered as an array of length 0 and == is comparing [ ] to its length.Why is [1,2]==2 false?
It's not comparing against the length of the array, it's calling the valueOf function of the object, which (I think) is the same as arr.join('').
console.log(String([].valueOf()) === '');
console.log(String([1, 2].valueOf()) === '1,2');
The first one results in '', which is loosely equal to 0.
The second one results in '1,2'.
the == will do some conversation before do the comparison
console.log([] == false) // true
console.log(0 == false) // true
console.log([] == 0) // true
also I suggest you read this question and its answers

In an if statement in javascript what is the 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?
In some JavaScript if statements I've seen the use of === as opposed to the standard ==.
What is the difference between these? and should I be using either one of them over the other?
e.g.
if (variable == 'string') {
return;
}
compared to:
if (variable === 'string') {
return;
}
=== also checks to be equal by type
For instance 1=="1" is true but 1==="1" is false
The === is a strict comparison (also checks type), whereas the == does a more relaxed comparison.
For instance:
var a = 'test';
if (a == true) {
// this will be true
}
if ( a === true) {
// this will not be true
}
Another example:
var b = '0';
if ( b == 0){
// this will be true
}
if ( b === 0 ){
// this will not be true
}
In particular it is very important when comparing falsy values. In Javascript all the following will be treated as false with relaxed comparison:
* false
* null
* undefined
* empty string ''
* number 0
* NaN

Categories