3 ms·
The way I would describe an array to a novice is that it's a more complex and potentially useful type of variable, similar to the ones you've been making so far
by WiseWeasel 15y ago
The way I would describe an array to a novice is that it's a more complex and potentially useful type of variable, similar to the ones you've been making so far, only instead of having a single "slot" for data, they are organized in a way that allows for multiple slots for information, and you can reference each slot by their position in the array, starting with position 0 (0, 1, 2, etc. describing the first, second and third. etc. positions in the array).
An array is a variable declared with square brackets around all the data, with a comma in between (delimiting) each positioned slot of data.
Example:
var array = ["data1", "data2", "data3"]
In this case, we've created an array containing text strings. It could just as easily contain integers, or as you will see demonstrated a little later in the course, other variables, including other arrays.
Then, in order to retrieve or modify a piece of information from the array, you reference the array variable with the array position you want in square brackets.
Example: To get the "data1" string from the above array, use the variable like this:
array[0]
To get an alert dialog with the "data2" string, the code would look like:
alert(array[1])
You can easily modify a piece of data in the array using this technique. For example, to change the string "data3" in the array to "third", the code would be:
array[2] = "third"
If you would like to add a new piece of data to the array, you could do the following:
array[3] = "fourth"
Note that this would require that you know the size of the array and that the position [3] is an available slot. A simpler method of adding data to an array is to use the .push function as such:
array.push("fourth")
- zds 15y agoThanks so much for the feedback. This lesson needs some improvements and we'll be shaping it up over the next few days.
- buff-a 15y agoI need some milk! Let me add that to my shopping array. Back in the real world, I see non techies deal with lists every day. Ordered lists. Given a list, with numbers in the margin, they can easily answer "Who came first?" or "What's next on the list?", or "What's item 5 on the agenda". But noooo. We have to call them arrays. Lists are something else. o_O The way I would describe an array to a novice is that it's a more complex and potentially useful type of variable Fuck me no!!!! Shut up Moss!
- WiseWeasel 15y agoThat's because arrays do get more complicated than flat lists like the example I gave above. You can actually have multiple organized data points in each position of the array as well, making arrays function more as a 2D relational database than as a flat list of data. For example, I could make an address book array with multiple data points as such: var addressbook = [{"firstname": "John", "lastname": "Doe", "phone": "555-555-5555", "email": "johndoe@host.com"}, {"firstname": "Jane", "lastname": "Smith", "phone": "666-666-6666", "email": "janesmith@host.com"}] Then, I can retrieve a piece of information like Jane's phone number like this: var janesnumber = addressbook[1].phone Arrays are very powerful tools, especially once you start referencing other arrays in them, as it allows complex organization of data without always needing to resort to a database solution, with a whole new language to learn.