Arrays in Javascript

Array

The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.

Initializing an Array

var count = ["one", "two", "three", "four"];

Initializing while declaring

var count = new Array(10, 20, 30, 40, 50);

Array Properties

length

The length property gives the number of elements in the array.

const count = ["one", "two", "three", "four"];
console.log(count);

Output:
4

Array Methods

  • push:

    it adds the element at the end of an Array. As array in JavaScript are mutable object, we can easily add or remove elements from the Array. And it dynamically changes as we modify the elements from the array.
const count = ["one", "two", "three", "four"];
count.push("five");
console.log(count);

Output:
["one", "two", "three", "four", "five"]
  • unshift:

    It adds elements at the front of an Array.
const count = ["one", "two", "three", "four"];
count.unshift("zero");
console.log(count);

Output:
["zero", "one", "two", "three", "four"]
  • pop:

    It removes the elements from the end of an array.
const count = ["one", "two", "three", "four"];
count.pop();
console.log(count);

Output:
["one", "two", "three"]
  • shift:

    It removes the elements from the beginning of the array.
const count = ["one", "two", "three", "four"];
count.shift();
console.log(count);

Output:
["two", "three", "four"]
  • splice:

    It adds or removes elements from an array.
const count = ["one", "two", "three", "four"];

// remove 2 first elements
let removed = arr.splice(0, 2);

console.log(removed)

Output:
["three", "four"]
  • slice:

    It extracts a section of the calling array and returns a new array.
const count = ["one", "two", "three", "four"];

let removed = arr.slice(1, 3);

console.log(removed)

Output:
["one", "four"]
  • concat:

    This returns a new array that is the calling array joined with other array(s) and/or value(s).
const count = ["one", "two", "three", "four"];
let result = count.concat(["five", "six"]);
console.log(result);

Output:
["one", "two", "three", "four", "five", "six"]
  • sort:

    Sorts the elements of an array in place and returns the array.
const count = ["one", "two", "three", "four"];
count.sort();
console.log(count);

Output:
["four", "one", "three", "two"]
  • reverse:

    It reverses the order of the elements of an array in place.
const count = ["one", "two", "three", "four"];
count.reverse();
console.log(count);

Output:
["four", "three", "two", "one"]
  • map:

    It returns a new array containing the results of invoking a function on every element in the calling array.
const count = ["one", "two", "three", "four"];
const result = count.map((el)=>(
   el + " " + el 
))
console.log(result);

Output:
["one one", "two two", "three three", "four four"]
  • reduce:

    It executes a user-defined callback function on each element of the array, to reduce it to a single value.
const count = ["one", "two", "three", "four"];
const result = count.reduce((sum, el)=>(
   sum + el 
))
console.log(result);

Output:
"onetwothreefour"
  • filter:

    It returns a new array containing all elements of the calling array for which the provided filtering function returns true.
const count = ["one", "two", "three", "four"];
const result = count.filter((el)=>(
   el == "three" ? el : null 
))
console.log(result);

Output:
"three"