Table of contents
- Creating JavaScript arrays
- Accessing JavaScript array elements
- Basic operations on arrays
- push() adds an element to the end of an array
- pop() removes an element from the end of an array
- unshift() adds new elements to the beginning of an array and returns the new length
- shift() removes the first element of an array and returns that element
- indexOf() finds an index of an element in the array
- isArray() or instanceof check whether an object is an array
- reverse() reverses the order of the elements in an array. It overwrites the original array
- includes() returns true if an array contains a specified value
- concat() joins two or more arrays and returns a new array, containing the joined arrays.
- join() returns an array as a string.
- slice() selects from a given start, up to a (not inclusive) given end
In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index:
A JavaScript array has the following characteristics:
First, an array can hold values of mixed types (number, string, boolean, null, undefined, object ...)
Second, the size of an array is dynamic and auto-growing. In other words, you donβt need to specify the array size up front.
Creating JavaScript arrays
JavaScript provides you with two ways to create an array. The first one is to use the Array
constructor as follows:
const arr1 = new Array('π','π½','π₯', ...);
const arr2 = Array('π','π½','π₯', ...);
JavaScript allows you to omit the new
operator when you use the Array() constructor.
The more preferred way to create an array is to use the array literal notation:
const arr = ['π','π½','π₯', ...];
Accessing JavaScript array elements
JavaScript arrays are zero-based indexed. To access an element in an array, you specify an index in the square brackets []
. The following shows how to access the elements of the arr
array:
const arr = ['π','π½','π₯'];
console.log(arr[0]); // 'π'
console.log(arr[1]); // 'π½'
console.log(arr[2]); // 'π₯'
const arr = ['π','π½','π₯'];
arr[2] = 'π';
console.log(arr); // [ 'π', 'π½', 'π' ]
Use the length
property to get the size of an array.
const arr = ['π','π½','π₯'];
console.log(arr.length); // 3
Use delete
operator to delete an element in the array. It leaves undefined
holes in the array.
const arr = ['π','π½','π₯'];
delete arr[0];
console.log(arr.length) // 3
console.log(arr[0]) // undefined
console.log(arr); // [ undefined, 'π½', 'π₯' ]
Basic operations on arrays
push()
adds an element to the end of an array
let arr = ['π','π½','π₯'];
arr.push('π');
console.log(arr); // ['π','π½','π₯','π']
pop()
removes an element from the end of an array
const arr = ['π','π½','π₯'];
const lastElement = arr.pop();
console.log(lastElement); //π₯
unshift()
adds new elements to the beginning of an array and returns the new length
const arr = ['π','π½','π₯'];
const unshift = arr.unshift('π');
console.log(unshift) // 4
console.log(arr) // [ 'π', 'π', 'π½', 'π₯' ]
shift()
removes the first element of an array and returns that element
const arr = ['π','π½','π₯'];
const shift = arr.shift();
console.log(shift); // π
console.log(arr) // [ 'π½', 'π₯' ]
indexOf()
finds an index of an element in the array
const arr = ['π','π½','π₯'];
const index = arr.indexOf('π₯');
console.log(index); // 2
isArray()
or instanceof
check whether an object is an array
const arr = ['π','π½','π₯'];
console.log(Array.isArray(arr)); // true
console.log(arr instanceof Array); // true
reverse()
reverses the order of the elements in an array. It overwrites the original array
const arr = ['π','π½','π₯'];
arr.reverse();
console.log(arr); // [ 'π₯', 'π½', 'π' ]
includes()
returns true
if an array contains a specified value
const arr = ['π','π½','π₯'];
const include = arr.includes('π₯', 1); // Start the search at position 1
console.log(include) // true
concat()
joins two or more arrays and returns a new array, containing the joined arrays.
const arr1 = ['π','π½','π₯'];
const arr2 = ['π±','π°']
const arr = arr1.concat(arr2);
console.log(arr) // [ 'π', 'π½', 'π₯', 'π±', 'π°' ]
join()
returns an array as a string.
const arr = ['π','π½','π₯'];
const join = arr.join(', ')
console.log(join) // π, π½, π₯
slice()
selects from a given start, up to a (not inclusive) given end
const arr = ['π','π½','π₯','π±','π°'];
const slice1 = arr.slice(0, -2)
const slice2 = arr.slice(0, 3)
console.log(slice1) // [ 'π', 'π½', 'π₯' ]
console.log(slice2) // [ 'π', 'π½', 'π₯' ]