4

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 compare:
[1].slice(1) === []
it's always false. So my Question is, what does [1].slice(1) really return?

2
  • 1
    What are you trying to do? There is nothing to slice at index 1 Commented Aug 13, 2012 at 16:52
  • writing a lispy to javascript compiler and trying to translate (rest '(1)). Commented Aug 13, 2012 at 16:58

4 Answers 4

8

=== compares objects by references.
You're comparing two different array objects which are both empty.

If you want to check whether an array is empty, check whether .length === 0.

Sign up to request clarification or add additional context in comments.

Comments

1

That's not a problem of slice or ===.

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

That's because both == and === compare objects by reference

Comments

-1

[] === [] also returns false. [1].slice(1) does in fact return []

Comments

-1

You better check the length:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.