Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Append Something to an Array in JavaScript</title> </head> <body> <script> var persons = ["Harry", "John", "Clark"]; // Append a single value to persons array persons.push("Peter"); console.log(persons); // Prints: ["Harry", "John", "Clark", "Peter"] // Append multiple values to persons array persons.push("Rohn", "Alice"); console.log(persons); // Prints: ["Harry", "John", "Clark", "Peter", "Rohn", "Alice"] // Loop through persons array and display all the values for(var i = 0; i < persons.length; i++){ document.write("<p>" + persons[i] + "</p>"); } </script> <p><strong>Note:</strong> Please check out the browser console by pressing the f12 key on the keyboard.</p> </body> </html>