What is The Purpose of "For " & "Loop" in javascript?

4 replies
I could not understood the function and purpose of "for " & "Loop" in javascript. For what they are used .(and also increament , decreament etc)?
#javascript #loop #purpose
Avatar of Unregistered
  • Profile picture of the author Eric Michalsen
    Here is a basic explanation: https://www.w3schools.com/js/js_loop_for.asp

    It is used for walking, or iterating, through an array or object.

    Do you have a piece of code you are stuck on, or a process you can't get to work? Post it. Let's see if we can't get it to work.

    edit: code for fun

    I have an object called markers (lat/lon locations from an api).

    this is actually JS, but I used the php code viewer

    PHP Code:
    // Here I create the loop and iterate through it markers.length times
      
    for (var 0markers.lengthi++) {
       
    // confirm the individual marker has data
        
    if (markers[i]!=null) {
          
    // take that data and split it at the comma (lat,lon)
          
    var data markers[i].split(',');
         
    // assign the marker object the data and icon
          
    var marker L.marker([data[0],data[1]], {
              
    iconicon,
            });
          
    // add that marker to a layer
          
    allpins.addLayer(marker);
        }
      }

    // add that layer to a map
      
    map.addLayer(allpins); 
    hopefully that helps in the understanding
    {{ DiscussionBoard.errors[11190070].message }}
    • Profile picture of the author kashifriazghgh
      I got some knowledge but still could not understood completely . Can you give me a real example (like a reference of a website`s section where these functions/loops are used)
      {{ DiscussionBoard.errors[11190452].message }}
      • Profile picture of the author Eric Michalsen
        I supplied some code above as an example, but that may have made the matter worse. Allow me to back up and take this from a more agnostic angle.

        Let us pretend we have an online shopping cart, with items that can be added and removed by a visitor. Each item, or product, has a product identifier, a unique number for the product, and we will call it PID.

        As a visitor adds (or removes) products to (from) their shopping cart, in reality all they are doing is adding or removing PIDs from an array, one we will call shopping_cart. It would look something like this:

        var shopping_cart = [4, 7, 3, 25, 8];

        Every time we add or remove an item, that array grows or shrinks.

        Now the fun bit. We want to see what is in the cart, to either add up all the items, or show the product titles or images, we are going to iterate through the array and look at each product:
        (again, it says PHP but we are taking a more agnostic approach here:
        PHP Code:

        // here is our for() loop.
        // taking this example, shopping_cart.length = 4 (yes, there are 5 items in the array, but it starts with 0, so:

        (key => value)
        => 4
        => 7
        => 3
        => 25
        => 8

        for (var 0shopping_cart.lengthi++) {
          
        // do something with shopping_cart[i]
          // ... or ...
          // do something with 4
          // then loop
          // do something with 7
          // then loop
          // do something with 3
          // ... etc ...

        the for() loop walks through an array, allowing you to do something with each value individually
        {{ DiscussionBoard.errors[11190869].message }}
  • Profile picture of the author Mike Anthony
    to make it simple and not give other words you might not understand yet its just a way to go over some data or situations

    you have ten movie names in your database and you what to get them and have them lsited on your web page

    You'd use a "for" to go through and get them all printed out - for every item found it will print out the movie name


    or you wanted to count from 1 to 10 in your pgramming you'd use a loop

    0 incremented by 1
    1 incremented by 1
    2 incremented by 1
    3 incremented by 1
    4 and so on

    the loop will stop when your conditionis met - in this case when you ahve reached 10
    Signature

    {{ DiscussionBoard.errors[11212102].message }}
Avatar of Unregistered

Trending Topics