Array:
JavaScript arrays are resizable and can contain mix of different data types.
Array elements are accessed using their indices.
Index starts from 0.
Javascript array-copy operation create shallow copies which means it shares the reference point of the original array. So changes made either in the copy or original array, reflect changes in both.
🔹Multiple ways to declare an array
Using Array literal syntax:
let numbers = [1, 2, 3, 4, 5];
Using Array Constructor:
let numbers = new Array(1, 2, 3, 4, 5);
Using
Array.of()
method
let numbers = Array.of(1, 2, 3, 4, 5);
🔹Essential Array Methods in JavaScript
push(element)
:A method used to add one or more elements to the end of an array. This method modifies the original array. We can also push an array to another array.
const marvelHeros = ["IronMan", "SpidarMan", "Captain-America"]; const dcHeros = ["superman", "flash"]; marvelHeros.push("Deadpool"); // push 1 element marvelHeros.push("Black-Widow", "Thor"); // push 2 elements dcHeros.push(marvelHeros); // push an entire array console.log(marvelHeros); console.log(dcHeros); // Output: ["IronMan", "SpidarMan", "Captain-America", "Deadpool","Black-Widow", "Thor"] // Output: ["superman", "flash", ["IronMan", "SpidarMan", "Captain-America", "superman", "flash"]]
pop()
:A method used to remove the last element from an array. It also returns the removed element. It modifies the original array.
let numbers = [1, 2, 3, 4, 5]; // Removing the last element let removedElement = numbers.pop(); console.log(removedElement); // Output: 5 console.log(numbers); // Output: [1, 2, 3, 4]
unshift(element)
:A method used to add one or more elements to the beginning of an array. It modifies the original array.
let fruits = ['apple', 'banana', 'orange']; // Adding elements to the beginning of the array fruits.unshift('grapes', 'kiwi'); console.log(fruits); // Output: ['grapes', 'kiwi', 'apple', 'banana', 'orange']
shift()
:A method which removes an element from the beginning of an array and also returns the removed element. It modifies the original array.
let fruits = ['apple', 'banana', 'orange']; // Removing the first element let removedElement = fruits.shift(); console.log(removedElement); // Output: 'apple' console.log(fruits); // Output: ['banana', 'orange']
shift()
and unshift()
are expensive operation because they require reindexing of all elements in the array due to their operations at the beginning of an array.includes(searchElement)
:A method to check whether the specified element is present in an array or not. Returns
true
if present andfalse
on absent.const friendCircle = ["Kiran", "Deepika", "Aaditya", "Jyoti"]; console.log(friendCircle.includes("Deepika")); // Output: true console.log(friendCircle.includes("Prajakta")); // Output: false
indexOf(element)
:A method used to find the index of a specified element in an array. Returns -1 if element is not present in an array.
let numbers = [10, 20, 30, 40, 50]; console.log(numbers.indexOf(30)); // Output: 2 console.log(numbers.indexOf(70)); // Output: -1
join(separator)
:A method used to convert an elements of an array into string representation with custom separators. If you omit the 'separator' parameter, it defaults to using commas between elements.
let fruits = ['apple', 'banana', 'orange']; let result = fruits.join(', '); console.log(result); // Output: 'apple, banana, orange' let numbers = [1, 2, 3, 4, 5]; let numbersString = numbers.join(' - '); console.log(numbersString); // Output: '1 - 2 - 3 - 4 - 5'
slice(startIndex, endIndex)
:A method extracts a portion of an array and returns a new array containing the extracted elements. This method exclude the element present at given
endIndex
and also does not change the original array.let numbers = [1, 2, 3, 4, 5]; let slicedNumbers = numbers.slice(1, 4); console.log(slicedNumbers); // Output: [2, 3, 4] console.log(numbers); // Output: [1, 2, 3, 4, 5]
splice(startIndex, deletecount, addNewElement)
:A method performs operation similar to
slice()
method but only difference is that it modifies the original array. We can also add new elements using this method.let numbers = [1, 2, 3, 4, 5]; // Removing elements let removedElements = numbers.splice(1, 2); console.log(numbers); // Output: [1, 4, 5] console.log(removedElements); // Output: [2, 3] // Adding elements numbers.splice(2, 0, 6, 7); console.log(numbers); // Output: [1, 4, 6, 7, 5]
flat(depth)
:A method is used to flatten a nested array structure. It creates a new array that contains all the elements of original nested array.
Using
Infinity
as the depth parameter is particularly useful when you have arrays with unknown or deeply nested structures, and you want to ensure complete flattening of all nested arrays.let nestedArray = [1, 2, [3, 4]]; let flatArray = nestedArray.flat(1); console.log(flatArray); // Output: [1, 2, 3, 4]
Array.isArray(value)
:A method used to check whether given value is array or not. Return boolean value depending upon the result.
console.log(Array.isArray("Prajakta")); // Output: false console.log(Array.isArray([2, 8, 9])); // Ouput: true
Array.from(value)
:It allows you to convert array-like objects (such as strings or NodeList) or iterable objects (such as Map or Set) into arrays.
let str = 'hello'; let strArray = Array.from(str); console.log(strArray); // Output: ['h', 'e', 'l', 'l', 'o']
🔹Multiple ways to merge arrays
concat(array1, array2)
:A method used to merge two or more arrays. It returns a new array containing concatenated elements. It does not modify the original array.
let arr1 = [1, 2, 3]; let arr2 = [4, 5]; let arr3 = ['a', 'b']; let mergedArray = arr1.concat(arr2, arr3); console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 'a', 'b']
Spread operator method :
It allows you to easily copy array elements, merge arrays, pass function arguments, and more.
let arr1 = [1, 2]; let arr2 = [3, 4]; let mergedArray = [...arr1, ...arr2]; console.log(mergedArray); // Output: [1, 2, 3, 4]