Q:

How to Append Values to an Array in JavaScript

0

How to Append Values to an Array in JavaScript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the push() Method

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

The following example will show you how to append single or multiple values to an array in JavaScript.

<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>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Generate a Timestamp in JavaScript... >>
<< How to Dynamically Access Object Property Using Va...