Arrays

This lesson will look at how to work with arrays in JavaScript. We will see how we can create arrays, access elements of an array, and various other functionalities with arrays.

Introduction

This is a quick revision of Javascript arrays for the sake of completion. If you understand Javascript arrays already, it will be quite boring for you.

In most programming languages, an array is a linear collection of elements stored in sequential order. We can iterate over arrays using array indices. Array indices typically start from 0 and go up to 'Array Length - 1'. Such arrays are known as zero-based arrays. Javascript arrays are zero-based arrays as well.

However, one unique thing about Javascript arrays is that they are actually objects and indices are just property names in that object. Indices are usually numbers and so Javascript arrays convert these numerical indices to string keys and then assign them as object properties. Therefore, if you access array[0], you are actually accessing the property "0" of the object array.

Here's how we can create an array in javascript.

Press + to interact
var topics = [];

Initializing the array

You can also initialize the array by passing in the values when declaring the array. e.g. let's create an array of the first ten prime numbers.

2357111317192329
First 10 prime numbers
Press + to interact
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];

Adding elements to an existing

...
Access this course and 1400+ top-rated courses and projects.