Stack Exchange
Stack Overflow
Questions
Tags
Users
About
Stack Overflow
Public
Questions
Tags
Users
About
Why let a = [{}] and a.indexOf({}) -1?
Asked
Jul 05 '19 at 14:38
Active
Jul 05 '19 at 14:42
Viewed
45 times
0
IndexOf cannot find my object. Here is the
JsFiddle
let a = [{}] console.log(a.indexOf({}))
javascript
asked Jul 05 '19 at 14:38
TSR
17,242
27
93
197
6
Because `{} !== {}`. Try `a.indexOf(a[0])`.
–
jonrsharpe
Jul 05 '19 at 14:39
1
Every distinct object is `!==` to every other object.
–
Pointy
Jul 05 '19 at 14:39
When you compare objects it checks whether the two operands refer to the same object in memory. `indexOf` obviously use equality internally. `{} === {}` is `false`
–
Maheer Ali
Jul 05 '19 at 14:40
0 Answers
0