JavaScript Fundamentals
JavaScript Arrays
JavaScript Arrays
Section titled “JavaScript Arrays”Arrays are ordered collections that can hold multiple values.
Creating Arrays
Section titled “Creating Arrays”const arr = [1, 2, 3, 4, 5];const mixed = [1, "hello", true, null];const nested = [[1, 2], [3, 4]];Basic Operations
Section titled “Basic Operations”const fruits = ["apple", "banana"];
fruits.push("cherry"); // Add to endfruits.pop(); // Remove from endfruits.unshift("mango"); // Add to startfruits.shift(); // Remove from startfruits.length; // Get lengthArray Methods
Section titled “Array Methods”Transformation
Section titled “Transformation”const numbers = [1, 2, 3, 4, 5];
// map - transform each elementnumbers.map(x => x * 2); // [2, 4, 6, 8, 10]
// filter - keep matching elementsnumbers.filter(x => x > 2); // [3, 4, 5]
// reduce - combine into single valuenumbers.reduce((acc, x) => acc + x, 0); // 15Searching
Section titled “Searching”const arr = [1, 2, 3, 4, 5];
arr.find(x => x > 3); // 4 (first match)arr.findIndex(x => x > 3); // 3 (index of first match)arr.includes(3); // truearr.indexOf(3); // 2Iteration
Section titled “Iteration”const arr = ["a", "b", "c"];
arr.forEach((item, index) => { console.log(`${index}: ${item}`);});
for (const item of arr) { console.log(item);}Other Useful Methods
Section titled “Other Useful Methods”const arr = [3, 1, 4, 1, 5];
arr.sort((a, b) => a - b); // [1, 1, 3, 4, 5]arr.reverse(); // [5, 4, 3, 1, 1]arr.slice(1, 3); // [4, 3] (copy portion)arr.splice(1, 2); // Remove 2 items at index 1arr.join(", "); // "5, 4, 3, 1, 1"Spread & Destructuring
Section titled “Spread & Destructuring”const arr1 = [1, 2, 3];const arr2 = [4, 5, 6];
// Spreadconst combined = [...arr1, ...arr2]; // [1, 2, 3, 4, 5, 6]
// Destructuringconst [first, second, ...rest] = arr1;// first = 1, second = 2, rest = [3]Next Steps
Section titled “Next Steps”Continue to DOM Manipulation →
Tutorials