JavaScript Array Methods

In this tutorial:

Array at() Takes an integer value as an argument and returns the item at that index.
Array concat() Used to combine two or more arrays.
Array join() Returns a string by joining all of the elements in an array
Array toString() Returns a string by joining all of the elements in an array

 

Array at() 

The at() method takes an integer value as an argument and returns the item at that index.

Syntax:

at(index)

Parameters:

  • index: The index of the array element to be returned. The at() accepts positive or negative integers. The negative integer count back from the last item of the array.

Return Value:

  • The element in the array at given index.

The following code returns the item at index 2 (third element).

const cities = ["New York","Boston","Chicago","Miami","Orlando"];

console.log(cities.at(2));
//Output: Chicago

Now let's try at() with negative index. The following example returns the last element of an array. To access the last item of an array, -1 is passed as an argument. 

const cities = ["New York","Boston","Chicago","Miami","Orlando"];

// Returns last item of array
console.log(cities.at(-1));
//Output: Orlando

Let's try another example.

const cities = ["New York","Boston","Chicago","Miami","Orlando"];

// Returns second last item of array
console.log(cities.at(-2));
//Output: Miami

Array concat()

The concat() method is used to combine two or more arrays. The concat() returns a new array and it does not change the existing array(s).

Syntax:

arr1.concat(arr2, arr3, ..., arrN)

Parameters:

arr1: required.

arrN: Array(s) to concatenate into a new array.

Return Value:

A new Array instance.

The following example shows how to concatenate two arrays.

const arr1 = ['a','b','c'];
const arr2 = ['d','e','f'];

//Concatenate arr1 and arr2 into new array
const arr3 = arr1.concat(arr2);

console.log(arr3);
//Output: Array ["a", "b", "c", "d", "e", "f"]

The following example shows how to concatenate more than two arrays.

const arr1 = ['a','b','c'];
const arr2 = ['d','e','f'];
const arr3 = ['g','h','i'];

//Concatenate arr1 and arr2 into new array
const arr4 = arr1.concat(arr2,arr3);

console.log(arr4);
//Output: Array ["a","b","c","d","e","f","g","h","i"]

Array join() 

The JavaScript join() method returns a string by joining all of the elements in an array, separated by commas or a specified separator.

Syntax:

join(separator)

Parameter:

  • Seperator:  (Optional). If omitted, the default comma is used to separate each element from the array. Otherwise, specified separator is used. In any case, separator becomes the part of string.

Return Value:

  • String containing each array element separated by commas or specified separator.
const arr = ["Red","Green","Black"];

console.log(arr.join());
// Output: "Red,Green,Black"

The following example shows, how to return a string by joining elements of an array separated by '-' separator.

const arr = ["Red","Green","Black"];

// - is used as separator
console.log(arr.join('-'));
// Output: "Red-Green-Black"

Try another example. Here space is used as separator.

const arr = ["Red","Green","Black"];

// ' ' is used as separator
console.log(arr.join(' '));
// Output: "Red Green Black"

The following example shows how to use join() method on a sparse array.

const arr = ["Red","Green",,"Black"];

console.log(arr.join());
// Output: "Red,Green,,Black"

Array toString()

The toString() method in JavaScript returns a string by joining all of the elements in an array, separated by commas. It does not change the existing array.

Syntax

toString()

Return Value:

  • String containing each array element separated by commas. 

Try following example.

const arr = ['O','r','a','n','g','e'];

console.log(arr.toString());
// Output: "O,r,a,n,g,e"

Here, is another example.

const arr = ["Red","Green","Blue"];

console.log(arr.toString());
// Output: "Red,Green,Blue"

The following code shows how to join an array of text and numbers to a string.

const arr = ["House #",10,"Street", 4];

console.log(arr.toString());
// Output: "House #,10,Street,4"

This example shows how to join elements of a sparse array.

const arr = ["Red","Green",,"Black"];

console.log(arr.toString());
// Output: "Red,Green,,Black"