const arr = [44, 33, 66]; // Original array for all examples
// Create
const arr = [44, 33, 66]; // [44, 33, 66]
const arr2 = Array.from('hello'); // ['h', 'e', 'l', 'l', 'o']
const arr3 = Array.of(44, 33, 66); // [44, 33, 66]
// Add/Remove (MUTATES ORIGINAL)
arr.push(77); // [44, 33, 66, 77] - MUTATES
arr.unshift(11); // [11, 44, 33, 66] - MUTATES
arr.pop(); // [44, 33] (returns 66) - MUTATES
arr.shift(); // [33, 66] (returns 44) - MUTATES
arr.splice(1, 1, 99); // [44, 99, 66] - MUTATES
// Find (RETURNS VALUE)
arr.find(x => x > 50); // 66
arr.findIndex(x => x > 50); // 2
arr.includes(33); // true
arr.indexOf(33); // 1
// Transform (RETURNS NEW ARRAY)
arr.map(x => x * 2); // [88, 66, 132]
arr.filter(x => x > 40); // [44, 66]
arr.reduce((acc, x) => acc + x, 0); // 143
arr.flat(); // [44, 33, 66] (no nested arrays)
arr.flatMap(x => [x, x]); // [44, 44, 33, 33, 66, 66]
// Copy/Combine (RETURNS NEW ARRAY)
[...arr]; // [44, 33, 66]
arr.slice(); // [44, 33, 66]
arr.slice(1, 3); // [33, 66]
[...arr, 88, 99]; // [44, 33, 66, 88, 99]
arr.concat([88, 99]); // [44, 33, 66, 88, 99]
// Sort/Reverse (MUTATES ORIGINAL)
arr.sort(); // [33, 44, 66] - MUTATES
arr.sort((a, b) => b - a); // [66, 44, 33] - MUTATES
arr.reverse(); // [66, 33, 44] - MUTATES
// Test (RETURNS BOOLEAN)
arr.every(x => x > 30); // true
arr.some(x => x > 60); // true
Array.isArray(arr); // true
// Loops/Iterate (VARIOUS OUTPUTS)
arr.forEach((item, i) => console.log(item)); // Logs: 44, 33, 66 - NO RETURN
for (let i = 0; i < arr.length; i++) {} // Traditional loop
for (const item of arr) {} // Loops through values: 44, 33, 66
for (const index in arr) {} // Loops through indices: "0", "1", "2"
// String (RETURNS STRING)
arr.join(', '); // "44, 33, 66"
arr.toString(); // "44,33,66"
// Properties
arr.length; // 3
arr.length = 0; // [] - MUTATES (clears array)