JavaScript Arrays

JavaScript Arrays

Introduction to arrays in JavaScript

An array is a special way of storing and organizing data in programming. An array is an ordered collection of values. Arrays can also be defined as special variables that can store multiple values in an ordered fashion.

Characteristics of JavaScript arrays

  • JavaScript arrays are resizeable. In JavaScript the size of an array can grow continuously to contain any amount of data.

  • JavaScript arrays are zero-indexed. Data stored in a JavaScript arrays are automatically numbered starting from zero. These numbers indicate the location/position of the data in the array. The first element is numbered zero, the second element is numbered one and so on.

  • JavaScript arrays do not store data associatively. Data stored in JavaScript arrays cannot be indexed with strings.

  • JavaScript arrays can contain data of different types. Different data types can be stored in a JavaScript array.

Creating JavaScript arrays

JavaScript arrays are commonly created using array literals. An array literal is a list of zero or more expressions enclosed in a square bracket []. Each expression is called an array element. Array elements are in a JavaScript array must be separated by commas.

JavaScript arrays are commonly declared using the const keyword.

Examples of JavaScript array code snippets:

// initializing an empty array
const arr = [];
// an array containing names 
const names = [‘jake’, ’paul’, ’john’];
// an array containing numbers
const ages = [9, 24, 15];

Accessing JavaScript array elements

Recall JavaScript elements are automatically zero-indexed that is array elements are automatically numbered starting from zero(0). JavaScript array element can only be accessed by referencing their respective indexes using the bracket notation. The general syntax for accessing an array element is arrayName[index]. This way of accessing elements using square brackets is referred to as bracket notation. For example:

const names = [“johnny”, “doe”, “frank”]
// to access Johnny 
names[0]
// to access doe and stores it in person
Let person = names[1]

Array length property

Under the hood, JavaScript arrrays are special types of objects which posses some default properties. One of such default properties is the length property. The length property returns the total number of elements in an array. For example:

const names = [“johnny”, “doe”] 
console.log(names.length)
//—> 2

Basic JavaScript Array Methods

JavaScript provides several built-in array methods. Methods are predefined operations that can be performed on a JavaScript array. When working with arrays we may want perform simple tasks such as copying parts of the array, adding elements to the array, removing elements from the array, iterating the array and sorting the array. This article will look at basic methods to add and remove elements from a JavaScript array.

Adding elements to the end of an array - push()

To add an element to the end of an array: use the push(element) method. The push(element) method is a build in JavaScript method that adds an element to the end of an array and returns the index of the element. For example:

const names = [“johnny”, “doe”, “frank”] 
names.push(“max”) 
console.log(names) 
//->  [“johnny”, “doe”, “frank”, “max“]

Adding elements to the beginning of an array - shift()

To add an element to the beginning of an array use the shift(element)method. The shift(element) method is a built-in JavaScript method that adds an element to the beginning of an array and returns the index of the element. Code example:

const names = [“johnny”, “doe”, “frank”] 
names.shift(“max”) 
console.log(names) 
//->  [“max“,“johnny”, “doe”, “frank”, “max“]

Removing the last element of an array - pop()

To remove the last element of an array: use the pop() method. The pop() method is a build in JavaScript method that removes the last element in an array and returns it.

const names = [“johnny”, “doe”, “frank”] 
names.pop() 
console.log(names) 
//->  [“johnny”, “doe”]

Removing the first element of an array - unShift()

To remove the first element of an array use the unshift() method. The unshift() method is a build in JavaScript array method that removes the first element in an array and returns it.

const names = [“johnny”, “doe”, “frank”] 
names.shift() 
console.log(names) 
//->  [“doe”, “frank”]

That’s a wrap! Thank you for reading this post, I hope it was useful. Click the follow button for more helpful tips on using JavaScript and other web development technologies. Suggest topics you’d like to read about.

Contact me