Array.slice for a single element array

I am really confused by this. If I do something like this:
[1].slice(1)
it returns an empty array (in the chrome interactive console). But if I compare:
[1].slice(1) === []
he is always false. So my question is: what really returns [1] .slice (1)?

+4
source share
4 answers

=== compares objects by links.
You are comparing two different array objects that are empty.

If you want to check if the array is empty, check if .length === 0 .

+8
source

This is not a slice or === problem.

If you execute [1]==[1] , it returns false .

This is because both == and === compare objects by reference

+1
source

[] === [] also returns false. [1].slice(1) really returns []

-1
source

Better check the length:

 [1].slice(1).length; // falsey 
-1
source

All Articles