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"]
.
+ There are no comments
Add yours