Minds
It is Recommended to practice while you learn

Arrays

1) It Enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.

2) JavaScript arrays are resizable and can contain a mix of different data types.

3) JavaScript arrays are zero-indexed: the first element of an array is at index 0, the second is at index 1, and so on — and the last element is at the value of the array's length property minus 1.

4) JavaScript arrays are not associative arrays and so, array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using nonnegative integers (or their respective string form) as indexes.

Creating an Array

// creating an array


syntax :

const array_name = [item1, item2, ...];

const array_name = new Array(item1, item2, ...);

Accessing Array Elements

// Accessing an array element


syntax :

let var_name = array_name[index]; // index always starts from 0

const array_name = new Array(item1, item2, ...);

Changing an Array Element

// Changing an array element


syntax :

array_name[index_to_be_changed] = "new_value";

Array Meathods And Functions

// Changing an array element



1) . length {to find length}

syntax : array_name.length

ie. let arr = [1,2,3,4,5,6];

console.log(arr.length) // output :- 6




2) . push() {to insert element at last}

syntax : array_name.push(value_to_be_added)

ie. let arr = [1,2,3,4,5,6];

arr.push(8)

console.log(arr) // output:- [1,2,3,4,5,6,8]




3) . slice() {to insert element at last}

cant change original array

syntax : array_name.slice(from(inclusive),to(exlusive))

ie. let arr = [1,2,3,4,5,6];

console.log(arr.slice(2,4)) //output:- [3,4]




4) . splice() { changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. }

can change original array

syntax : array_name.splice(start)
             array_name.splice(start, deleteCount)
            array_name.splice(start, deleteCount, item1)
            array_name.splice(start, deleteCount, item1, item2, itemN)

ie. const months = ['Jan', 'March', 'April', 'June'];

months.splice(4, 1, 'May'); // Replaces 1 element at index 4

console.log(months); // output: ["Jan", "Feb", "March", "April", "May"]




5) . concat() {to merge 2 arrays}

syntax : array_name.cocat(arr1,arr2)

ie. let arr1 = [1,2,3,4,5,6];

let arr2 = [1,2,3,4,5,6];

let arr3 = []

console.log(arr3.concat(arr1,arr2)) // output :- [1,2,3,4,5,6,1,2,3,4,5,6]




6) . copyWithin() {to merge 2 arrays}

syntax :
array_name.copyWithin(target)
array_name.copyWithin(target, start)
array_name.copyWithin(target, start, end)

const array1 = ['a', 'b', 'c', 'd', 'e'];
// Copy to index 0 the element at index 3
console.log(array1.copyWithin(0, 3, 4)); // Expected output: Array ["d", "b", "c", "d", "e"]
// Copy to index 1 all elements from index 3 to the end
console.log(array1.copyWithin(1, 3)); // Expected output: Array ["d", "d", "e", "d", "e"]




7) . includes() {check value with given address}

syntax : array.includes(values,index)

let arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(arr1.includes(5, 4)); // output :- true




8) . indexof() {to find index of give value}

syntax : array_name.indexOf(value)

ie. let arr1 = [1,2,3,4,5,6];

console.log(arr1.indexOf(6)) // output :- 5




9) . lastindexof() {to find last occurance index of give value}

syntax : array_name.lastIndexOf(value)

ie. let arr1 = [1,2,3,4,7,3,5,7,5,6];

console.log(arr1.lastIndexOf(7)) // output :- 7




10) . shift() {to remove first value from an array}

syntax : array_name.shift()

ie. var array = ["GFG", "Geeks", "for", "Geeks"];
console.log(array.shift()); // output :- term removed = "GFG"
console.log(array) // output:-Geeks, for, Geeks



11) . pop() {to remove last value from an array}

syntax : array_name.shift()

ie. var array = ["GFG", "Geeks", "for", "Geeks"];
console.log(array.pop()); // output :- term removed = "Geeks"
console.log(array) // output:-GFG, Geeks, for



12) . reverse() {to reverse an array}

syntax : array_name.reverse()

ie. var array = ["GFG", "Geeks", "for", "Geeks"];
console.log(array.reverse()); // output :- [ 'Geeks', 'for', 'Geeks', 'GFG' ]



13) . sort() {to sort an array}

syntax : array_name.sort()

ie. var array = [5,3,2,5,7,2,4,5];
console.log(array.sort()); // output :- [2, 2, 3, 4,5, 5, 5, 7]



14) . tostring() {to convert an array into string form}

syntax : array_name.toString()

ie. var array = [5,3,2,5,7,2,4,5];
console.log(array.toString()); // output :- 5,3,2,5,7,2,4,5



15) . unshift() {to add value at first position}

syntax : array_name.unshift(value)

ie. var array = ["GFG", "Geeks", "for", "Geeks"];
console.log(array.unshift(5)); // output :- term added = 5
console.log(array) // output:-5,Geeks, for, Geeks



16) . split() {converts array into string}

syntax : array_name.split()

ie. let str = "javascript";
let ar1 = str.split("");
console.log(ar1);



17) . map() {operation applies on all values of array}

syntax : array.map(expr)

ie. let asqr = [1, 4, 9, 16];
console.log(asqr.map(Math.sqrt));