Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Use of arrays?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #162715
    javascriptnewbie
    Participant

    So I’m learning javascript and practicing Arrays concept right now. I’ve totally understood, how to make one and whatnot..

    Look at this code, I know what it does. The only question I’ve is, where would it be used for? Just putting in data in the array makes no sense what so ever to me..

    Code:
    box = [];
    for(i=0; i <=10; i++){
         box[i] = i;
         console.log(box[i]);
    }
    

    thankyou

    #162787
    nkrisc
    Participant

    Well your example demonstrates how arrays work but serves no practical purpose whatsoever. Are you looking for examples of when arrays are actually used in a useful way?

    #162811
    dyr
    Participant

    I believe you need to do more reading/learning about the fundamentals of programming in order to learn about when and why you would use a data structure such as an Array. There are countless resources for this level of information and a quick google search should get you started.

    With that said…..

    There’s quite a bit to say about Arrays in JavaScript and I would encourage you to read A LOT and keep experimenting. I believe as you learn more about programming this question will go away and you will see the usefulness of Arrays.

    As you probably know an array is, simply put, a list of items. They are a core data structure in JavaScript and many other programming languages. Technically, in JS they are list-like objects where the keys are positive integers and the length property is always updated to be the largest index + 1. OK, so an array is an object in JavaScript. This means it inherits methods from Object.prototype as well as Array.prototype. But it’s not JUST an object and modern browsers (where JS is compiled and executed) know this and perform many optimizations to increase performance. JavaScript optimizes code before it gets executed. Many people have bench-marked the performance of Arrays and Array methods compared to other means of achieving similar results. See this stack overflow thread.

    By choosing to store some data in an Array you have access to Array prototype functions such as .map(), .sort(), .reverse(), .splice(), .unshift(), .pop(), .push() and more which allow you to modify and work with that data more easily and efficiently.

    So, WHAT DO I USE AN ARRAY FOR!? Well first you need to have something specific in mind that you’re trying to accomplish. JavaScript is particularly useful and widely used for manipulating DOM elements (things on a web page). To keep things extra simple I’m just going to provide a basic example that doesn’t do any DOM manipulation.

    Here’s a dead simple example:

    // array filled with strings
    var thingsIWant = ['$1,000,000', 'a Ferrari', 'Wings']
    
    // a function that takes an array as its only argument
    var makeWishlist = function(wishlist) {  
      for( var i=0; i<wishlist.length; i++ ) {
        // just log this sentence with the items from wishlist filled in
        console.log("I wish I had " + wishlist[i] + "!") 
      }
    }
    
    // run the function and pass in the array
    makeWishlist(thingsIWant)
    

    The output from this will be:

    // in console
    I wish I had $1,000,000!
    I wish I had a Ferrari!
    I wish I had Wings!
    

    So you can see when you need to repeat some action on items in a list you use an Array.

    I hope that was helpful.

    TL;DR: Read more about the fundamentals of programming. Arrays in JavaScript are list-like objects with some built-in functionality and compiler optimizations.

    #162843
    noahgelman
    Participant

    Here’s a small example. Arrays are good at storing lists of information. Here is used makes of cars. But it can be used for things like a list of image paths, items on a page, etc.

    Pretty much anything that is a list of data.

    var cars = ['mustang', 'corvette', 'beetle']'
    
    for(i=0; i<cars.length; i++) {
      console.log('I love my ' + cars[i]);
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.