I want to check an object which is always an array. One important case is where it is an array equivalent to foo
below. But it could contain any number of integers. I need to evaluate the specific case in an if
statement. However, I find the behavior confusing:
let foo = [0]
foo === [0] // false
foo == [0] // false
foo === 0 // false
foo == 0 // true
How can I check whether foo
is the exact array of [0]
?
Thanks!
Edit: Thanks for your comments. Not sure if this really is a duplicate. In any case, thanks for the solutions. I went for if (foo instanceof Array && foo.length == 1 && foo[0] === 0)
.