Arrays Are Objects with Special Behavior

JavaScript arrays are objects with indexed properties and a special length property. They can contain values of different types and can be sparse. Array methods such as push, pop, slice, and splice differ in whether they mutate the original array.

Understanding mutation matters when working with React state, functional programming, caching, and shared data structures.

slice vs splice
const values = [1, 2, 3, 4];

const copy = values.slice(1, 3);
console.log(copy);
console.log(values);

values.splice(1, 2);
console.log(values);
💡 slice creates a new array without changing the source. splice removes or replaces elements in the original array.
Key Takeaways
  • Arrays are objects with special indexed and length behavior.
  • slice does not mutate the source array.
  • splice mutates the original array.
Knowledge Check

Which method mutates an array by removing or replacing elements?