DEV Community

Discussion on: JavaScript Array

Collapse
 
pentacular profile image
pentacular

I think it's wise to be clear and consistent regarding the terms you use.

There are several methods that you can create and change array: shift, unshift, push, pop, splice, concat, slice, rest operator, spread operator, destructing.
Mutation: You can use splice to mutate the array.

Mutation includes any change to the object, so shift, unshift, push, pop, and splice are all mutators.

Adding items: I used spread operator to add items at the begin, splice and slice for adding items in middle, push or spread at the end of the array.

The spread operator doesn't add items to an array -- it includes them in the construction of a new array.

Splice and push do add items, but slice and spread don't.

So these are quite different kinds of thing and it's a bit misleading to lump them together.

Removing from the end of the array
Removing items from the middle of the array

Slice doesn't remove items from an array -- it copies a subset of items from an array to produce a new array.

The examples here are of extraction rather than removal.

You use reduce when adding sum of the array.

Reduce isn't limited to addition.


I would be careful to distinguish between construction and mutation, since these have very different consequences.

Good luck.

Collapse
 
huyddo profile image
Huy Do

Thanks for your comments.