Can I be sure that array.slice() will always work the same as array.slice(0)?
I've always used the .slice() method with no arguments to make a copy of a
JavaScript Array. This works fine in every browser and JavaScript
environment I've tried: it is treated the same as .slice(0).
It's just a matter of style, but to me omitting the start argument
entirely makes it more clear that we aren't taking any special sub-slice
of the array but want a copy of the whole thing.
However, both MDN and MSDN say that the first argument to array.slice() is
required. Only the second argument is optional. Other online sources such
as TutorialsPoint and W3Schools say the same thing. (No, I'm not
recommending W3Schools! Just pointing out that they agree with MDN and
MSDN on this issue.)
Have I just been lucky with this? Could there be a browser or other
JavaScript environment where array.slice() doesn't work?
No comments:
Post a Comment