PART 1 - Introduction to JavaScript arrays

PART 1 - Introduction to JavaScript arrays

Β·

3 min read

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:

  1. First, an array can hold values of mixed types (number, string, boolean, null, undefined, object ...)

  2. 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)   // [ 'πŸ“', '🌽', 'πŸ₯‘' ]
Β