Which array method in JavaScript is used to insert an element at the beginning of an array ?
Show Hint
To remember this: push/pop work on the end of the array, whereas unshift/shift work on the beginning of the array. The longer word in each pair adds elements (push, unshift), while the shorter word removes elements (pop, shift).
Step 1: Reviewing Common JavaScript Array Methods:
JavaScript provides standard methods on the Array.prototype to manipulate elements at different ends of an array. Let's analyze their operations:
Step 2: Evaluating the Options:
• (A) push(): Adds one or more elements to the end of an array and returns the new length of the array.
• (B) unshift(): Adds one or more elements to the beginning of an array and returns the new length of the array.
• (C) concat(): Is used to merge two or more arrays. This method does not mutate the existing arrays, but instead returns a brand-new array.
• (D) pop(): Removes the last element from an array and returns that element.
Therefore, unshift() is the correct choice to insert at the beginning.