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
Related
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);
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
This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 12 months ago.
I'm facing a if condition issue in javascript. Please have look on below code.
1.
const a = 1;
if(a && a > -1)
{
console.log('suceess')
}
else
{
console.log('failed')
}
Here in if condition it is returning true.
2.
const a = 0;
if(a && a > -1)
{
console.log('suceess')
}
else
{
console.log('failed')
}
Here in if condition it is returning Zero.
I'm not getting this. Can someone please explain why is it so.
in Javascript 0 is considered a "falsy" value, which mean il will be evaluate to false when used in a condition like if (0) {}.
See https://developer.mozilla.org/en-US/docs/Glossary/Falsy for more information about falsy values.
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);
}
This question already has answers here:
Difference between "empty' and "undefined" in Javascript? [duplicate]
(2 answers)
Closed 3 years ago.
What are all the methods that can do that?
I only know of two methods. To summarize, to find whether arr[1] is empty or undefined, use
1 in arr
or
Object.keys(arr).includes("1")
but the second method may possibly create a big array to begin with.
// running inside of Node 6.11.0
// Methods that works:
> a = Array(3)
[ , , ]
> b = Array.from({length:3})
[ undefined, undefined, undefined ]
> 1 in a
false
> 1 in b
true
> Object.keys(a).includes("1")
false
> Object.keys(b).includes("1")
true
// Method that doesn't
> a[1]
undefined
> b[1]
undefined
> a[1] === undefined
true
> b[1] === undefined
true
You can use hasOwnProperty:
const arr = [true,undefined,,true];
console.log('undefined', arr.hasOwnProperty('1'));
console.log('hole', arr.hasOwnProperty('2'));