How to Prepend a String to an Array in JavaScript?

Estimated read time 1 min read

To prepend a string to an array in JavaScript, you can use the unshift() method, which adds one or more elements to the beginning of an array and returns the new length of the array. Here’s an example:

let fruits = ["banana", "apple", "cherry"];
fruits.unshift("orange");
console.log(fruits);

In the example above, the unshift() method is used to add the string "orange" to the beginning of the fruits array. The result is logged to the console and will output ["orange", "banana", "apple", "cherry"].

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply