How can you add a new item to the beginning of an array?

  • arr.unshift(newItem);
  • arr.push(newItem);
  • arr.append(newItem);
  • arr.insert(0, newItem);
To add a new item to the beginning of an array in JavaScript, you can use the arr.unshift(newItem); method. This will insert newItem at the start of the array, shifting the existing elements to the right. push() adds to the end of the array, append() and insert() are not standard array methods.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *