Working with Arrays in JavaScript

February 5, 2022

In this article on Working with Arrays in JavaScript, I will explain how to create and use arrays in JavaScript. Certainly, you will find lots of examples of JavaScript arrays here.

For the purpose of creating an array, we can initialize it with values. Also, we can use the Array() constructor. The following examples demonstrate how to create arrays.

var a1=[2, 5, 9, 8, 4, 10];
var a2=[12, 18, 'abc', 90.8, 1, 2, 'hello'];
var a3=Array(6,1,3);
var a4=Array(10);

In order to find a simple example of creating an array and displaying its elements, click here:

Working with Arrays in JavaScript Using Built-In Methods

concat(): This method helps us in combining two or more arrays to create a single large array.

join(): In order to join the elements of an array by using a separator, we use the join method.

push(): Basically, this method inserts an element at the last position in an array.

pop(): Similarly, the pop() method displays the last element from the array and removes it.

reverse(): In order to reverse the elements in an array we use the reverse() method.

sort(): Likewise, the sort() method sorts the elements of an array in ascending order.

indexOf() method returns the index of the first occurrence of the specified element in the array. In case, the element is not there, it returns -1.

Likewise, the lastIndexOf() method starts searching the array starting from the last element and searches backward. In case, the element is not there, it returns -1.

Further Reading

Evolution of JavaScript from ES1 to ES2020

Introduction to HTML DOM Methods in JavaScript

JavaScript Practice Exercise

Understanding Document Object Model (DOM) in JavaScript

What is Asynchronous JavaScript?

JavaScript Code for Event Handling

Callback Functions in JavaScript

Understanding JSON Web Tokens

Arrow Functions in JavaScript

JavaScript Code Examples

Understanding HTTP Requests and Responses

Show or Hide TextBox when Selection Changed in JavaScript

Changing Style of HTML Elements using getElementsByClassName() Method

Creating Classes in JavaScript


You may also like...