Array In Javascript

Array In Javascript

Array

In Javascript, Array is an object that stores multiple items under a single variable name. It is used when we want to store a list of elements and access them by a single variable. There is a pair of square brackets [ ] required to symbolize the array in javascript, all the elements present inside an array are separated by (,) and you can create an array by using elements of datatype String, Numbers, Boolean, etc.

Syntax

let array_name = ["item1", "item2", "item3"];

Example

let org = ["LCO", "Ineuron", "Findcoder", "Youtube"];
console.log(org);

Output

[ 'LCO', 'Ineuron', 'Findcoder', 'Youtube' ]

Access Each Element of an Array

To access an individual element of an array, use the name of the array name followed by the index of the element in square brackets.

Example

let org = ["LCO", "Ineuron", "Findcoder", "Youtube"];
console.log(org[0]);
console.log(org[1]);

Output

LCO
Ineuron

Methods in Array

1.Push()

This method adds new elements at end of the array.

Example

let org = ["LCO", "Ineuron", "Findcoder", "Youtube"];
org.push("Coder Community", "Hashnode");
console.log(org);

Output

[
  'LCO',
  'Ineuron',
  'Findcoder',
  'Youtube',
  'Coder Community',
  'Hashnode'
]

2.pop()

This method removes new elements that are added end of the array.

Example

let org = ["LCO", "Ineuron", "Findcoder", "Youtube", "Coder Community", "Hashnode"];
org.pop();
console.log(org);

Output

[ 'LCO', 'Ineuron', 'Findcoder', 'Youtube', 'Coder Community' ]

3.concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays but instead, it returns a new array.

Example

let arr_1 = [ 'LCO', 'Ineuron', 'Findcoder']
let arr_2 = ['Youtube', 'Coder Community'];
console.log(arr_1.concat(arr_2));

Output

[ 'LCO', 'Ineuron', 'Findcoder', 'Youtube', 'Coder Community' ]

4.slice()

The slice() method returns a portion of the array into a new array object selected from start to end(excluding end) and both start and end represent an index of items.

Example

let orgs = [ 'LCO', 'Ineuron', 'Findcoder', 'Youtube', 'Coder Community' ]
console.log(orgs.slice(1, 4));

Output

[ 'Ineuron', 'Findcoder', 'Youtube' ]

5.splice()

This splice() method can add a new item to an array. The very first parameter defines the position, the second one defines how many items should be removed and the third one defines what items should add to the first parameter's position.

Example

let orgs = [ 'LCO', 'Ineuron', 'Findcoder', 'Youtube', 'Coder Community' ]
orgs.splice(1, 3, "Hitesh");
console.log(orgs);

Output

[ 'LCO', 'Hitesh', 'Coder Community' ]

This map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

6.map()

The map() creates a new array by calling a function for every array element. It does not execute the function for empty elements, it doesn't change the original array and moreover, it doesn't call more than once for each element of an array.

Example

let arr1 = [1, 2, 3, 4];
let arr2 = arr1.map(x => x + 2);
console.log(arr2);

Output

[ 3, 4, 5, 6 ]

7.filter()

This filter() method returns a filtered new array, that satisfies functions requirement.

Example

let orgs = [ 'LCO', 'Ineuron', 'Findcoder', 'Youtube', 'Coder Community' ]
let result = orgs.filter(word => word.length > 7);
console.log(result);

Output

[ 'Findcoder', 'Coder Community' ]

8.reduce()

This reduce() method executes a reducer function on each element of the array and returns a single output value.

Example

let arr1 = [1, 2, 3, 4];

let initialValue = 0;
let sumWithInitial = arr1.reduce(
  (previousValue, currentValue) => previousValue + currentValue
);
console.log(sumWithInitial);

Output

10