JS: Array | JavaScript

JavaScript Array

Array data type allows to store multiple values in just one variable instead of multiple variables. The values are mostly of same type

Why to use Array?

If we don't use Array, this is how we have to use a data-type with multiple variables.

var continent1 = "Asia"; var continent2 = "Europe"; var continent3 = "Africa"; var continent4 = "North America"; var continent5 = "South America"; var continent6 = "Australia";

Using Array in JavaScript

The continents can be used in a single variable in JavaScript.

var continents = ["Asia", "Europe", "Africa", "North America", "South America", "Australia"];

continents is the variable of type Array, storing 6 string values Asia, Europe, Africa, North America, South America, Australia called elements of the array.

Array can be appended.

continents.push("Antarctica"); console.log(continents); 7 [ "Asia", "Europe", "Africa", "North America", "South America", "Australia", "Antarctica" ]

Another continent is added at the end of the array continents. The function push returns the elements count (called length) in the array.

Remove the last element

var last = continents.pop(); console.log(last); console.log(continents); Antarctica [ "Asia", "Europe", "Africa", "North America", "South America", "Australia" ]

The last element is removed through pop function. The function pop returns the element removed from the array continents.

var arr = ["Earth", 7, 3.14, true, {"name":"Abc Xyz", id:12345}];
  • JavaScript arrays index are 0 based i.e. First item of the array is 0, Second item is 1 and so on.

Array are Truly

JavaScript array always truly i.e. result true in logical conditions e.g. if and Ternary operator (?:). Even that, JavaScript empty array [] results true.

var arr = []; if(arr) alert("Truly"); else alert("Falsy"); Truly

length

length property of array returns the number items in the array.

var arr = ['First','Second','Third'] alert(arr.length);

concat

concat method returns the new array consiting of the both first and second array

var arr1 = ['First','Second','Third']; var arr2 = ['Last']; var arr = arr1.concat(arr2); //[ "First", "Second", "Third", "Last" ] alert(arr); First,Second,Third,Last

copyWithin

entries

every

fill

filter

find

findIndex

flat

flatMap

forEach

var arr = ['First','Second','Third']; arr.forEach(function(item, index, array){ console.log(item, index, array)} );

forEach method visits the each item in the Array. Here:
item is the item being iterated, index is the index of the item being iterated in the array, array is the array to whom the item belongs to.

includes

indexOf

var arr = ['First','Second','Third']; var idx = arr.indexOf('Third'); alert(idx); //2 idx = arr.indexOf('Fourth'); alert(idx); //-1 2 -1

join

keys

lastIndexOf

var arr = ['First','Second','Third','Third','Fifth']; var idx = arr.lastIndexOf('Third'); alert(idx); //3 idx = arr.lastIndexOf('Fourth'); alert(idx); //-1 3 -1

map

pop

pop removes and returns the last item from the array

var arr = ['First','Second','Third']; var item = arr.pop(); alert(item); //Third Third

push

push adds the element at the end of the array and returns the index of the added item.

var arr = ['First','Second','Third']; var i = arr.push('Fourth'); alert(i); //4 4

reduce

reduceRight

reverse

reverse change direction of elements reverse.reverse changes the original array.

var arr = ['First','Second','Third']; arr.reverse(); //[ "Third", "Second", "First" ] alert(arr); // Third,Second,First

shift

shift removes and returns the first item from the array

var arr = ['First','Second','Third']; var item = arr.shift(); alert(item); //First First

slice

slice returns the nth item of given range of the array. Original array remains intact.

var arr = ['First','Second','Third']; var item = arr.slice(0,2);//[ "First", "Second" ] alert(item); First,Second

some

sort

sort returns the new array sorted with the elements in ascending order.

var arr1 = ['d','b','a',98,'c',6,9]; var arr2 = arr1.sort(); // [ 6, 9, 98, "a", "b", "c", "d" ] alert(arr2); [ 6, 9, 98, "a", "b", "c", "d" ]

splice

unshift

values

valueOf