最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array.slice on array with one element - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question edited Aug 13, 2012 at 20:13 LOZ 1,1772 gold badges16 silver badges46 bronze badges asked Aug 13, 2012 at 16:49 rabrarabra 7561 gold badge6 silver badges11 bronze badges 2
  • 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
Add a ment  | 

4 Answers 4

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
发布评论

评论列表(0)

  1. 暂无评论