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 — even 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, . . . Each subsequent number is the sum of the previous two.

Creating an array object

There are 3 ways of creating arrays.

Create an array object

 

Create an Array object by instantiating the Array object.

create an Array object by inserting collection data

Inserting data

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

push

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.

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.

Accessing array values

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

pop

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

Additional 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.

length

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

concat

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.

reverse

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

splice

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




Application example

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.

 

Our console should print out

Updating data

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

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.

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

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
π