JavaScript Arrays

Javascript has an Array object for storing and manipulating data in applications.  JavaScript Arrays are container-like values that can hold other values, Essentially JavaScript arrays are used to store multiple values in a single variable..

The values inside an array are called elements. The element values within a JavaScript Array don’t all have to be the same type of value. Elements can be any kind of JavaScript value and even contain other arrays!.

In the examples below I will create a simple module in Node.js to develop a Fibonacci sequence  number generator to help illustrate how and why you may want to use JavaScript array’s in your software application.  

The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .

Information

In the Fibonacci sequence, each subsequent number is the sum of the previous two

Creating a JavaScript array objects

There are 3 ways of creating arrays.

1. Create an array object

JS

2. Create an Array object by instantiating the Array object.

JS

3. Create an Array object by inserting collection data

JS

Inserting data into a JavaScript Array

After creating an Array object, we can insert data. Use [] with index if you want to assign the value.

JS

JavaScript Array push method

The Array push method adds an element to the array and returns the array’s length.

In the example below we use the push() function to insert data.

JS

In the example below we’ll use the push method to add an element to an array and then log the count of elements in the array to the console.

JS

Accessing JavaScript array values

To access array data, you can use [] with data index parameter.

JS

JavaScript Array popmethod

The array pop method removes the last element in the array and returns that element’s value.

JS

Additional JavaScript Array Properties and Methods

Arrays have additional built-in variables and functions, which are also known as properties and methods, that provide addtional value when working with arrays in code.

JavaScript Array length function

An array’s length property provides the number of elements contained within the array.

JS

JavaScript Array concat function

The Array concat method returns a new array that combines the values of two arrays. Which is useful if you want to join two arrays to each other.

JS

JavaScript Array reversefunction

The array reverse method returns a copy of the array in opposite order.

JS

JavaScript Array splice function

The splice() method adds/removes items to/from an array, and returns the removed item(s).

JS

An application example of JavaScript Arrays

I’ll create a simple node.js application which will illustrate how to make use of arrays.

The code below includes a reference to simple Fibonacci sequence generator class which exposes two methods to generate a Fibonacci sequence, Looping or Recursion,  the source code for the class is included in the GitHub Repository.

JS

Our console window should look something similar to the below with all our values contained in the array printed out

Shell

Updating data

To update an item of array data, you can use [] with data index and thus assign a new value.

JS

Removing data

You can use the pop() function to remove data from the Array.
The pop() method removes the last element from an array and returns that element.

If you want to remove data by specific index then you can use the splice() function.

JS

Summary

Arrays are simply data types that can store more than one variable. Each variable is stored in an array element.

Arrays are usually Zero-bound so the first element in the array is always 0.

As programmer you will be required to develop and implement algorithms for a variety of tasks, As tasks become more complex algorithm development is facilitated by structuring or organizing data in specialized ways.

Arrays are suitable data structures that can be selected for the specific task Some data structures are provided by programming languages others must be derived by the programmer from available data types and structures.

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

Gary Woodfine

Technical Director at Denizon
Gary is Technical Director at Denizon, an independent software vendor specialising in IoT, Field Service and associated managed services,enabling customers to be efficient, productive, secure and scalable in a way which helps them address and reduce their ecological impact.

Denizon's product line successfully integrate IoT, Artificial Intelligence and Blockchain technology to enable efficient, productive, secure and scalable solutions to help organisations address increasing energy demands, ecological impact and Health & Safety concerns of their staff.

Latest posts by Gary Woodfine (see all)