Example: Array in JavaScript
<script>
var artists = ['Prince','Sam Cooke','Steely Dan','Neil Young'];
len = artists.length;
document.write("");
for (i = 0; i < len; i++) {
document.write("- " + artists[i]);
}
document.write("
");
</script>
Output:
Example: Array in JavaScript - Adding and Sorting Elements
In this example, a new element is added to the array with the push method and then the the array is sorted.
<script>
var artists = ['Prince','Sam Cooke','Steely Dan','Neil Young'];
artists.push("Maxwell");
artists.sort();
len = artists.length;
document.write("");
for (i = 0; i < len; i++) {
document.write("- " + artists[i]);
}
document.write("
");
</script>
Output: