Array cheatsheet

What is an array?
An Array is a collection of elements and values at once. It is used when we want to store a list of elements and access them by a single variable.
Array index starts from 0, so the first element will be at zero index. We can access the single value by giving index number. We write an array inside square brackets.
Declaration of an array with an example
let arr1 = [1,"priti",true,56];
console.log(arr1);
Output
[1,'priti',true,56]
Let's see how we can fetch single element by indexing
let arr1 = [1,"priti",true,56];
console.log(arr1[0]);
Output
1
Array methods:
1)Adding element in array: To add element at the end of an array we use .push() method.
let arr1 = [1,"priti",true,56];
arr1.push("sam",70);
console.log(arr1);
Output
[1,'priti',true,56,'sam',70]
2)Slice method: The .slice() method gives a copy of array with the elements that we mentioned in the method by passing the index. It will not override original array.
Syntax:array.slice(starting index, the index of element till we want the element +1)
Ex:arr1.slice(1,4)
let arr1 = [1,"priti",true,56];
console.log(arr1);
console.log(arr1.slice(1,3));
Output
['priti',true]
3)Splice method: The .splice() method override original array, by removing and replacing existing elements and adding new elements on which this method is applied.
Syntax: array.splice(starting index, delete count, [replacing element])
The replacing element replace the number that is mentioned in delete count.
Suppose deleteCount=2 and replacing element is "priti" then priti will replace two elements and instead of those two elements only one "priti" will be there.
let arr1 = [1,"priti",true,56];
console.log(arr1.splice(1,2,"komal");
Output
[1,'komal',56]
4)Concatenation of an array: To join/add two or more array we can use .concat() method.
Syntax: `array.concat()
let arr1 = [1,"priti",true,56];
console.log(arr1.concat(3,"komal");
Output
[1,'priti',true,56,3,'komal']
5)We can copy an array inside an array by.copyWithin() method
Syntax:array.copyWithin( target_index,start,end);
let arr1 = [1,2,3,4,5,6,7,8,9];
console.log(arr1.copyWithin(1,5,7));
Output
[1,6,7,4,5,6,7,8,9]
6)If we want to check whether some value is present or not in an array it will check by array.included() method
Syntax:array.includes(value tht u want to check,index tht u want to match)
let arr1 = [1,2,3,4,5,6,7,8,9];
console.log(arr1.includes(5,4));
Output
true
7)To find length of an array by array.indexOf() method
Syntax:array.indexOf(value you want to check index of)
let arr1 = [1,"priti",true,56];
console.log(arr1.indexof(1));
Output
0
8)In map method what you write (inside this) condition will apply to all the values of array array.map()
Syntax:array.map(condition)
let arr1 = [1,4,9,16];
console.log(arr1.map(Math.sqrt));
Output
[1,2,3,4]
9)Removing elements from the beginning of the array we use array.shift()method
let arr1 = [1,4,9,16];
console.log(arr1.shift());
Output
[4,9,16]
10)To sort the array in an ascending order we have array.sort() method
let arr1 = [2,5,3,9,21,76,34];
console.log(arr1.sort());
Output
[2,3,5,9,21,34,76]
11)To sort the array in reverse order we have array.reverse() method.
let arr1 = [2,4,5,7,9];
console.log(arr1.reverse());
Output
[9,7,5,4,2]
12)To add the values at the beginning of the array by array.unshift() method
let arr1 = [1,4,9,16];
console.log(arr1.unshift(25));
Output
[25,1,4,9,16]
13)We can split the word into alphabets by array.split()
let arr1 = ["sahil"];
console.log(arr1.split(""));
Output
['s', 'a', 'h', 'i', 'l']
14)Removing elements from the end of the array by array.pop()
let arr1 = [2,5,8,1,4];
console.log(arr1.pop());
Output
[2,5,8,1]
15)Array fill means it will fill all the values by one element by array.fill() method
let arr1 = [3,6,1,24,56,7];
console.log(arr1.fill(0));
Output
[0,0,0,0,0,0]
This was the article on arrays that covers the most of the array methods.
Thank you for reading!!
Happy Learning!!




