Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Looping an Array in Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 115
    Comment on it

    Hello reader's in this blog we are going to discuss about looping, by using array we repeat the names inside a loop. Using loop is easy to access all the elements of an array.

     

    By using a for loop, it is the common access to accessing an array.

     

    Syntax:-

    var mammals = new Array("cat","dog","human","whale","seal");
        var animalString = "";
        for (var i = 0; i < mammals. length; i++) {
            animalString += mammals[i] + " ";
        }
        alert(animalString);

     

    We need to get every element of the array so we use for loop for looping. The default start of the array is from "0" and by using array property length to set at the end of the loop.

     

    When ever you dont want to get every element of the array. In that situation you can traverse an array till than you can find the specific element.


    In these situation, you want to use a while loop and test the array elements:

     

    Syntax:-

    var numArray = new Array(1,4,66,123,240,444,555);
    var i = 0;
    while (numArray[i] < 100) {
        alert(numArray[i++]);
    }

     

    Note:- Index counter "i" is incremental as it is used to access an array element. The existing element value of "i" is accessed first and then the variable is incremented by using the "i++.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: