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
Related
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.
This question already has answers here:
How to compare arrays in JavaScript?
(61 answers)
How to check if an array is empty or exists?
(23 answers)
Closed 12 months ago.
address.trans returns [] when theres no transactions pend
if(txs === []){
returns a transaction still
Any solutions? Basically registering a "depo" when I never depo'd to the generated wallet.enter image description here
This should be happening because an array is an object. And two objects even with the same value are not equal in JS because they have different reference.
You can see that below:
let trans = [];
console.log(trans === []);
console.log([] === []);
console.log(trans.length === 0);
Array.prototype.length can be one way to check it:
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).
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?
This question already has answers here:
How do I check if an object has a specific property in JavaScript?
(31 answers)
Closed 6 years ago.
Imagine this scenario:
var myObject = {
"1030":{},
"1059":{}
}
I want to check if 1030 is in that object.
How would I do this?
Try hasOwnProperty
if(myObject.hasOwnProperty("1030")) {
// Do code
}
It's a bit safer than checking if(myObject["1030"]). This will return false, if the value is falsey (false, undefined, null), which may be desirable, but also does not strictly mean it does not exist.