Arrays 101: A Beginner’s Guide

A JavaScript array is a global object that is used in the construction of arrays, which are high-level, list-like objects.

An array is an ordered collection of data types that have a numeric index attached to them. Arrays can store any data type, including strings, numbers, and even other objects. The length of an array can be changed after it has been declared.

You can add or remove elements from an array at any time. If you need to store a list of items, then using an array would be your best bet since you can easily access and change the items in the list. So, look at javascript arrays.

Arrays are created with a pair of square brackets [ ] around a list of comma-separated values. The first value in the array is at index 0; the second value is at index 1, and so on. Accessing a specific element in an array can be done with square bracket notation as well. Take a look at the following example to see how this works:

Now,  let fruits = [‘oranges’, ‘apples’, ‘bananas’];

  console.log(fruits[0]); // oranges

  console.log(fruits[1]); // apples

  console.log(fruits[2]); // bananas

We declared a variable named fruit and set it equal to an array with three different strings inside of it. We then logged the first, second, and third elements to the console using square bracket notation and got back ‘oranges,’ ‘apples,’ and ‘bananas,’ respectively.

As you can see, accessing specific elements in an array is simple once you know how they are indexed.

Adding or removing elements from an array is just as easy using built-in methods that come with every array object. To add an element to the end of an array, you can use the .push() method like this:

  Thus, let fruits = [‘oranges’, ‘apples’, ‘bananas’];

  fruits.push(‘peaches’);

  console.log(fruits); // [‘oranges’, ‘apples’, ‘bananas’, ‘peaches’]

This adds ‘peaches’ onto the end of our fruits array giving us a new length of 4 items instead of 3. If we wanted to add an element to the start of our fruits array, we could use the .unshift() method like this:

Hence,  let fruits = [‘oranges’, ‘apples’, ‘bananas’];

  fruits.unshift(‘strawberries’);

  console.log(fruits); // [‘strawberries’, ‘oranges’, ‘apples’, ‘bananas’]

This adds ‘strawberries’ onto the start of our fruits array resulting in a new length of 4 items instead of 3 yet again but with ‘strawberries’ now being at index 0 instead of ‘oranges’ like before. Thus, look at arrays in javascript.

Removing elements from an array works similarly using either .pop() or .shift(). These methods work by taking off and returning the last or first element from an array, respectively, leaving us with a new shorter length for our given array. Check out how this works below:

  let fruits = [‘oranges’, ‘apples’, ‘bananas’];

  let removedFruit = fruits.pop(); // removes bananas and returns it

  console.log(removedFruit); // bananas

  console.log(fruits); // [‘oranges’, ‘apples’]

  let anotherRemovedFruit = fruits.shift(); // removes oranges and returns it

  console,.log(anotherRemovedFruit); // oranges

  console,.log(fruits);// [‘apples’]

As you can see, these methods make adding or removing elements from arrays very simple, which comes in handy quite often when working with lists containing data types such as strings or numbers since order matters in those cases.

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *