Home > Development > Javascript Arrays

Javascript Arrays

So here is how to arrays in Javascript. I have not gone into depth on all the array methods in this post like the last on C Sharp. I am going to check out the functions provided by JQuery in terms of providing foreach and the like.

I do like how arrays in JS are dynamic and you can add elements to the end.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    <title>Examples 2</title>
    <script type="text/javascript">

        function addElementToArray(farray,value){ // Arrays are passed by reference
            farray[farray.length] = value;

        function printLastItem(farray){
            document.writeln(farray[farray.length - 1]);
        var items1 = new Array(10); // Declare array with 10 elements
        var items2 = []; // Create empty array with zero element equal to undefined

        // Add elements to the arrays and then print them to the browser
        for(i = 0; i < 10; i++){
            items1[i] = i;
            items2[i] = i;

        for(j = 0; j < items1.length; j++){
            document.writeln('items1 value at position ' + j + ' is ' + items1[j] + '<br/>');

        for(k = 0; k < items2.length; k++){
            document.writeln('items2 value at position ' + k + ' is ' + items1[k] + '<br/>');




  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: