JavaScript array.includes weird behavior with objects [duplicate] - javascript

This question already has answers here:
Comparing objects in JavaScript
(10 answers)
Closed 2 years ago.
Can somebody explain this weird behavior of javascript on comparing the existence of an object in an array

Equality checks work different for objects than for strings or numbers:
console.log('hello' === 'hello');
console.log(2 === 2);
console.log({x:2} === {x:2});

Related

Types matter in array.prototype.includes(), but it is not intuitive in JavaScript? [duplicate]

This question already has answers here:
do loose equality comparision using array.includes
(2 answers)
How does the Array.prototype.includes function compare objects
(5 answers)
Closed 4 months ago.
While writing a sudoku solver, I ran into a issue which took me hours to solve which is based on ['1','2','3'].includes(3). is false. Since 3 in includes is a type number but 3 in array is type string. But this is against the intuition that type coercion happens while comparing values in js? Should it be corrected in js?
ex: ['1','2','3'].includes(3) // false
and ['1','2','3'].includes('3') // true
Throw some light on whether this is needed or should be fixed as it will lead to errors in programs if not checked carefully.

Javascript "in" give wrong results [duplicate]

This question already has answers here:
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
What is the difference between "in operator" and "includes()" for JavaScript arrays
(4 answers)
Closed 2 years ago.
I have the following code in Javascript:
var myArray = {"a":21600904, "b":21100999, "c":21602019, "d":21704354, "e":21602271, "f":21500123};
console.log(Object.values(myArray));
console.log(21600904 in Object.values(myArray));
And I get the following output:
[ 21600904, 21100999, 21602019, 21704354, 21602271, 21500123 ]
false
But this was not what I expected. What I understand is, 21600904 is inside Object.values(myArray) but 21600904 in Object.values(myArray) returns false instead of true.
What am I missing here? Why this code does not print true?

Issue working with comparison operator [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Triple equal signs return false for arrays in javascript. why?
(4 answers)
Closed 4 years ago.
Console logging this.byPassViewState returns ["01"]
if i do this.byPassViewState === ['01'] it returns false
typeof(this.byPassViewState) retuns object
My question is why this.byPassViewState returns false ? it suppose to be true right ? please tell me what i'm doing wrong here
You cannot compare 2 array with just using == or === operators.
The easiest way to compare array is using JSON.stringify().
let byPassViewState = ["01"];
let compare = (JSON.stringify(byPassViewState) == JSON.stringify(["01"]) );
console.log(compare);
Please Reference : How to compare arrays in JavaScript?

Why === and == giving false for following? [duplicate]

This question already has answers here:
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Closed 4 years ago.
I know it is very much stupid to ask but can anyone tell me
Why === and == giving false for following.
x=[[1,2]];
console.log(x[0]===[1,2]);
console.log(x[0]==[1,2]);
Here typeof(x[0]) and typeof([1,2]) is also same, then why it is giving false?
Because they are different values in memory.
x=[[1,2]];
console.log(x[0]===[1,2]); // Here you're creating a new array in memory
console.log(x[0]==[1,2]); // Here you're creating a new array in memory
var y = x[0]; //Same value in memory
console.log(x[0]===y);
console.log(x[0]==y);
Equality comparisons and sameness

How do we find the length of an object in Javascript? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
I dont see how we can find the length of an object. For arrays i can your array.length but it doesnt work for objects, any suggestions?
Thanks!
Just like that:
Object.keys(objectName).length;

Categories