Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Javascript Arrays Explained

When do you need an array?

When you think storing or keeping track of multiple data values (e.g. saving the following five grades: 73, 98, 86, 61 and 96) think data structures. The most common and popular data structure are arrays.

What are arrays?

As I have mentioned before, Arrays are one type of data structure. Data structure being a ‘container’, ‘bucket’ or ‘storage unit’ that allows you to store multiple data values. The structure or ‘setup’ of an array is pretty straightforward. The first item is stored at ‘location’ or index 0, the second item is stored at ‘location’ or index 1 and the next one at 2 and so on. Arrays can help you with doing things like calculating the average of a set of numbers. You can store different kind of data types in an array. So maybe a set of numbers or a set of strings. Most programming languages restrict you stick to one type for each array you create. So no mix matching. However javascript is a little more ‘chill’ about that rule.

How do they work?

Let’s use Javascript to demonstrate how arrays work and learn a little more about them. Here what we need to get started:

  • CREATE – We need to create an array, either empty or with items already inside.
  • SAVE – We need to ‘store’, ‘save’ or ‘assign’ the array to a variable so we can reference it.

Once we do those two things we can work with the data.

Creating an Array

Every language does it differently. Here is how javascript does it.

// Syntax:
 
var array_name = [item1, item2, ...];      

// Concrete example using numbers:

var grades = [73, 98, 86, 61, 96]; 

// Concrete example using strings:

var names = ['James', 'Mohammad', 'Toby', 'Leslie', 'Diane'];

How to access the data (members)

Simple:


// array_name[location/index]

names[0]; // is referring to 'James' 

grades[3]; // is referring to 61 

// Let's save the variable to a variable

var name = names[0];
var grade = grades[3];

Popular Confusions

The biggest confusion when it comes to arrays is related to distinguishing the name of the variable/array, index and value/member.

Syntax: name_of_array[index] = value;

The name is just what we use to refer to any particular array. It can be any string you choose.

The index in a regular primitive array will always be an integer ranging from 0 up to the size of the array – 1. All arrays start at index 0.

The value is what is stored at a particular index in an array.

So keep those three separate. If you are ever confused ask yourself is the issue related to separating the name, index and value?

What you can do with arrays.

Before I finish this post on arrays I would like to quickly go over some of the things you can do with arrays.

  • Iterate over an array
  • Resize an array
  • Replace/add/remove a member of an array
  • Rename an array
  • Use built-in functions/methods to
    • Display all content of an array
    • Combine all members into one string
    • Squeeze new members into the array
    • You can combine multiple arrays into one
    • Plus a few more fancy stuff

I really hope this helps you understanding arrays a little better. Please let me know if you are still confused about arrays or if there are other aspects you would like me to discuss.

The post Javascript Arrays Explained appeared first on Coding Simplified.



This post first appeared on Coding Simplified, please read the originial post: here

Share the post

Javascript Arrays Explained

×

Subscribe to Coding Simplified

Get updates delivered right to your inbox!

Thank you for your subscription

×