A JavaScript array is a structured collection of values, referred to as elements, each associated with a numeric index that begins at 0. The first element resides at index 0, the second at index 1, and so forth. Arrays are highly adaptable, capable of storing diverse data types like numbers, strings, objects, or even other arrays.
Array Basics
JavaScript arrays can be created using array literals [] for simplicity and efficiency or the new Array() constructor.
Array Literal :
- Use [] for simplicity and efficiency.
JavaScript
let arr = []; // Empty array
let arr2 = [10, 20, 30]; // Array with mixed values
console.log(arr);
console.log(arr2);
Array Constructor :
Use new Array().
JavaScript
let arr = new Array(10, 20, 30); // Array with values
console.log(arr);
Now we start Basic Operations on JavaScript Arrays after reading Array Basics
Basic Operations on JavaScript Arrays
JavaScript array operations include accessing elements with arr[index], accessing length with arr.length, modifying elements via assignment, adding with push() or unshift(), removing with pop(), shift(), or splice(), or combining arrays with concat() and map(), filter(), reduce().
JavaScript array operations
Accessing Elements:
Use index (arr[index]).
- First element: arr[0]
- Last element: arr[arr.length - 1]
JavaScript
let arr = [10,20,30,40]
// Access first element
console.log(arr[0])
// Access last element
console.log(arr[arr.length-1]);
Array Length :
Use arr.length to get/set size.
JavaScript
let arr = [10,20,30,40];
console.log(arr.length);
Modifying Elements :
Assign new value to an index.
JavaScript
let arr = [10,20,40];
//Modifiying element
arr[2]=30;
console.log(arr);
Adding Element :
- push() : Add to end.
- unshift() : Add to start.
JavaScript
let arr = [20,40,60];
// Add Element last
arr.push(80);
//Add Element First
arr.unshift(0);
console.log(arr);
Output[ 0, 20, 40, 60, 80 ]
Removing Elements :
- pop() : Remove from end.
- shift() : Remove from start.
- splice(start, deleteCount) : Remove/replace elements from specific index.
JavaScript
let arr = [10,20,30,40,50,60];
//Remove End Element
arr.pop();
//Remove Start Element
arr.shift();
console.log(arr);
let arr2 = [20,40,60,80];
// Remove start to end
arr2.splice(1,3);
console.log(arr2);
Output[ 20, 30, 40, 50 ]
[ 20 ]
Concatenation:
arr1.concat(arr2) combines arrays.
JavaScript
let arr1 = [1,2,3,4];
let arr2 = [5,6,7,8];
//Using concat to combin arr
console.log(arr1.concat(arr2));
Output[
1, 2, 3, 4,
5, 6, 7, 8
]
Map, Filter, and Reduce :
- map(): Creates a new array by applying a function to each element.
- filter(): Creates a new array with elements that pass a test (returns true).
- reduce(): Reduces array to a single value by applying a function to each element.
JavaScript
let numbers = [1, 2, 3, 4, 5];
// Map
let doubled = numbers.map(num => num * 2);
console.log(doubled);
// Filter
let evens = numbers.filter(num => num % 2 === 0);
console.log(evens);
// Reduce
let sum = numbers.reduce((total, num) => total + num, 0);
console.log(sum);
Output[ 2, 4, 6, 8, 10 ]
[ 2, 4 ]
15
Now we start Some Problems on Array after reading Basic Operations on JavaScript Arrays.
Some Problems on Array
Array Reversal :
Reverse the order of elements in-place by swapping elements from both ends.
JavaScript
let arr = [10,20,30,40,50];
function reverseArray(arr) {
let left = 0, right = arr.length - 1;
while (left < right) {
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
return arr;
}
console.log(reverseArray(arr));
Output[ 50, 40, 30, 20, 10 ]
Kadane’s Algorithm :
Find the maximum sum of a contiguous subarray in O(n) time.
JavaScript
let arr = [10,20,40,60,80];
function maxSubarraySum(arr) {
let maxSoFar = arr[0], currMax = arr[0];
for (let i = 1; i < arr.length; i++) {
currMax = Math.max(arr[i], currMax + arr[i]);
maxSoFar = Math.max(maxSoFar, currMax);
}
return maxSoFar;
}
console.log(maxSubarraySum(arr));
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics