var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
for (i=0;i<scripts.length;i++)
{
document.write(scripts[i] + "<br >");
}
Output
PHP
ASP
JavaScript
HTML
array_name.length
So this number we will set as upper limit of our number of rotation or looping.
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
for (i=0;i<scripts.length;i++)
{
document.write(scripts[i] + "<br >");
}
The for loop in the above code loops through the elements of the array and display one by one vertically by adding one line break at the end of each element. The upper limit of the for loop is set to script.length value which is in this case equal to 4
<script>
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
document.write( scripts.toString());
</script>
Output is
PHP,ASP,JavaScript,HTML
<script type="text/javascript">
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
var str=scripts.join(" : ");
document.write(str);
//document.write(scripts.join(" <br> "));
</script>
The output of the above code is here
PHP : ASP : JavaScript : HTML
document.write(scripts[2]); // Output is JavaScript
document.write(scripts[scripts.length-1]); // Output is HTML
document.write(scripts[0]); // Output is PHP
add or remove elements at any position of an array by using splice method
<script>
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
for (var key in scripts) {
document.write("key : " + key + " =>value: " + scripts[key] + "<br>");
}
</script>
Output
key : 0 =>value: PHP
key : 1 =>value: ASP
key : 2 =>value: JavaScript
key : 3 =>value: HTML