I'm 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 pare:
[1].slice(1) === []
it's always false. So my Question is, what does [1].slice(1) really return?
I'm 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 pare:
[1].slice(1) === []
it's always false. So my Question is, what does [1].slice(1) really return?
- 1 What are you trying to do? There is nothing to slice at index 1 – Esailija Commented Aug 13, 2012 at 16:52
- writing a lispy to javascript piler and trying to translate (rest '(1)). – rabra Commented Aug 13, 2012 at 16:58
4 Answers
Reset to default 8===
pares objects by references.
You're paring two different array objects which are both empty.
If you want to check whether an array is empty, check whether .length === 0
.
That's not a problem of slice
or ===
.
If you do [1]==[1]
, it returns false
.
That's because both ==
and ===
pare objects by reference
[] === []
also returns false. [1].slice(1)
does in fact return []
You better check the length:
[1].slice(1).length; // falsey