Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Add New Array Elements at the Beginning</title> </head> <body> <script> var fruits = ["Apple", "Banana", "Mango"]; // Prepend a single element to fruits array fruits.unshift("Orange"); console.log(fruits); // Prints: ["Orange", "Apple", "Banana", "Mango"] // Prepend multiple elements to fruits array fruits.unshift("Guava", "Papaya"); console.log(fruits); // Prints: ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"] // Loop through fruits array and display all the values for(var i = 0; i < fruits.length; i++){ document.write("<p>" + fruits[i] + "</p>"); } </script> <p><strong>Note:</strong> Please check out the browser console by pressing the f12 key on the keyboard.</p> </body> </html>