Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can array.splice() be called with one argument?

Tags:

javascript

According to MDN, I think, array.splice can take 1 argument:

If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed.

but it's not clear whether the one argument option is a SpiderMonkey extension (there's only one syntax example, which is confusing).

It works in Chrome and Firefox, but I don't know of the compatibility beyond that. Does anybody know definitively?

like image 234
Skilldrick Avatar asked Oct 27 '25 06:10

Skilldrick


2 Answers

According to 15.4.4.12 of the ECMAScript specification, the only mentioned prototype is:

Array.prototype.splice (start, deleteCount [ , item1 [ , item2 [ , … ] ] ] )

So no, the second parameter is not optional in my reading.

like image 61
Alexander Gessler Avatar answered Oct 28 '25 18:10

Alexander Gessler


Per spec, at least two arguments are required.

The ability to call with only one argument is a SpiderMonkey extension to the spec. It's entirely possible that Chrome implemented a similar extension. Looks like so did IE9, Opera, and Safari (just tested in those).

Maybe it's time for a spec change....

like image 28
Boris Zbarsky Avatar answered Oct 28 '25 20:10

Boris Zbarsky