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.
Loading...
Related Quiz
- What is the primary use of a "for" loop in JavaScript?
- You have a block of code that needs to be executed when multiple conditions are true. Which control structure should be used to optimize the code for readability and performance?
- JavaScript was initially designed to make web pages more _________.
- How do you declare a two-dimensional array in JavaScript?
- You're debugging a piece of code and find an unexpected type coercion in a comparison. Which operator is most likely being used that could cause this issue?