JavaScript

How to Add Elements to an Array in JavaScript

You can simply use the push() method to add values to an array.

The following example will show you how to add one or more values to an array in JavaScript.
 

How to Add Elements to an Array in JavaScript
var languages = ["JavaScript", "CSS", "HTML"];
 
// Add a single value "PHP" to the languages array
languages.push("PHP");

console.log(languages);
// Output: ["JavaScript", "CSS", "HTML", "PHP"]
 
// Add multiple values to the languages array
languages.push("Python", "Java");
 
console.log(languages);
// Output: ["JavaScript", "CSS", "HTML", "PHP", "Python", "Java"]
 
// Display the array of languages and display all the values
for(var i = 0; i < languages.length; i++){
    document.write("" + languages[i] + "");
}
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *