- Forums
- Javascipt
- How To Make Javascript Arrays
Showing You Making Arrays Using Javascript To Make A Simple Javascript Array And Then Display The Centent Of Each Element On The Webpage [2198], Last Updated: Mon Jun 24, 2024
Webune Support
Sun Dec 27, 2009
2 Comments
465 Visits
so lets say you want to create an array in javascript and then be able to display each value for each array element, its very simple, there are many ways to create arrays in javascript, here are some examples:
regular array example (add an optional integer argument to control array's size)
var Colors=new Array();
Colors[0]="red";
Colors[1]="green";
Colors[2]="blue";
this is a condensed array:
JAVASCRIPT CODE:
var Colors=new Array("red","gree","blue");
example literal array
JAVASCRIPT CODE:
var Colors=["red","green","blue"];
now try it yourself, we put together a sample code:
JAVASCRIPT CODE:
<script type="text/javascript">
var Colors=new Array(); // regular array (add an optional integer
Colors[0]="red"; // argument to control array's size)
Colors[1]="green";
Colors[2]="blue";
document.write(Colors[0]);
</script>
next step: the best way to display all the elements of an array if you want to list them, its to use a loop learn how on our next tutorial:
http://www.java2s.com/Code/JavaScript/Form-Control/UsingtheformelementsArray.htm
https://www.webune.com/forums/how-to-make-javascript-arrays.html