Arrays: Defining, Using

[Total: 0    Average: 0/5]

This file shows how to create a JavaScript array, how to assign values into it, and some of the impacts of doing these different methods.This is a READ only field.

Body Code:

<script type=text/javascript>
var a = new Array();      // length of array "a" is 0
var b = new Array(10);    // length of array "b" is 10, elements are [0] thru [9] and all are undefined
var c = new Array(1,2,3); // length of array "c" is 3, elements are [0],[1],[2] and set to 1,2,3 respectively
var d = [4,5];            // length of array "d" is 2, elements [0],[1] are set to 4,5 respectively

// length of array "fruits" is 6.
var fruits = ["mango", "banana", "cherry", "pear", "apple", "orange"]
document.write('<div class=code>'+'var fruits = ["mango", "banana", "cherry", "pear", "apple", "orange"]'+'</div>')
document.writeln("<div class=desc><BR>A simple loop goes thru and shows the content of the array.<BR></div>")
document.writeln("<div class=code>for (var i = 0; i < fruits.length; i++ )<BR></div>")
var sp = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
document.write('<div class=result>')
for (var i=0;i<fruits.length;i++) document.writeln(sp+"fruits["+i+"]="+fruits[i]+"<BR>");
document.write("</div><HR>");
document.write("<div class=desc>"join()" is a method for every array.<BR><BR>")
document.write("This function returns a string that is equal to the contents of the entire array dilemited by a comma(s).<BR></div>")
document.writeln("<BR><span class=code>fruits.join()</span> <span class=desc>returns</span> <span class=result>"+fruits.join()+"</span><BR><hr>");
document.write("<div class=desc>"reverse()" is another method for every array.<BR>It takes the array order and flips it.</div>")
fruits.reverse();
document.writeln("<BR><span class=code>fruits.reverse()</span> <span class=desc>returns</span> <span class=result>"+fruits.join()+"</span><BR><hr>");
document.write("<div class=desc>"sort()" is another method for every array.<br>If invoked with no arguments sorts alphabetically</div>")
fruits.sort();
document.writeln("<br><span class=code>fruits.sort()</span> <span class=desc>returns</span> <span class=result>"+fruits.join()+"</span><BR><HR>");
document.write("<div class=desc>"sort()" with a paramter of the function that should be used to do the sort:</div>")
fruits.sort(function(a,b){return a<b});
document.writeln("<br><span class=code>fruits.sort(function(a,b){return a < b})</span> <span class=desc>returns</span> <span class=result>"+fruits.join()+"</span>");
fruits.sort(function(a,b){return a<b});
document.writeln("<br><span class=code>fruits.sort(function(a,b){return a < b})</span> <span class=desc>returns</span> <span class=result>"+fruits.join()+"</span>");
fruits.sort(function(a,b){return a>b});
document.writeln("<br><span class=code>fruits.sort(function(a,b){return a > b})</span> <span class=desc>returns</span> <span class=result>"+fruits.join()+"</span>");
fruits.sort(function(a,b){return a>b});
document.writeln("<br><span class=code>fruits.sort(function(a,b){return a > b})</span> <span class=desc>returns</span> <span class=result>"+fruits.join()+"</span><BR><HR>");
document.write("<div class=desc>"concat()" adds a new item to the end of the array</div>")
fruits=fruits.concat("plum")
document.writeln("<BR><span class=code>fruits.concat('plum')</span> <span class=desc>returns</span> <span class=result>"+fruits.join()+"</span><BR><HR>");
document.write("<div class=desc>"slice()" returns a section of the array specified.</div>")
document.writeln("<br><span class=code>fruits.slice(0,3)</span> <span class=desc>returns</span> <span class=result>"+fruits.slice(0,3).join()+"</span>");
document.writeln("<br><span class=code>fruits.slice(3)</span> <span class=desc>returns</span> <span class=result>"+fruits.slice(3).join()+"</span>");
document.writeln("<br><span class=code>fruits.slice(1,-1)</span> <span class=desc>returns</span> <span class=result>"+fruits.slice(1,-1).join()+"</span>");
document.writeln("<br><span class=code>fruits.slice(-3,-2)</span> <span class=desc>returns</span> <span class=result>"+fruits.slice(-3,-2).join()+"</span><BR><HR>");
document.write("<div class=desc>"push()" and "pop()" are only supported by Netscape, and are not shown here in the HTML</div>")
document.write("<br><div class=desc>Example usage would be:</div>")
document.write("<div class=code>fruits.push("peech")</div><br><hr>")
document.write("<div class=desc> "toString()" returns different text in InternetExplorer and Netscape.  Care should be used.</div>")
document.writeln("<br><span class=code>fruits.toString()</span> <span class=desc>returns</span> <span class=result>"+fruits.toString()+"</span>")
</script>

Times Viewed: 1

Leave a Reply

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