Arrays
Functions & Data
Preview
Arrays
const fruits = ["mango", "pawpaw", "pineapple"];\nfruits[0]; // "mango"\nfruits.length; // 3Adding & removing
fruits.push("orange"); // add to end\nfruits.pop(); // remove from end\nfruits.unshift("banana"); // add to front\nfruits.splice(1, 1); // remove at index 1Looping
for (const fruit of fruits) {\n console.log(fruit);\n}\n\nfruits.forEach((f) => console.log(f));Common methods
fruits.map(f => f.toUpperCase()); // transform each\nfruits.filter(f => f.length > 5); // keep matches\nfruits.find(f => f.startsWith("m")); // first match\nfruits.includes("mango"); // true\nfruits.join(", "); // "mango, pawpaw, ..."Mutating vs new
push/pop/splice change the original. map/filter/slice return new arrays. Know which you are using.
Tip:map,filterandreducedo the heavy lifting in modern JS. Learn them early and you will write far less loop code.
This is a free preview lesson
Enroll in the JavaScript Fundamentals to unlock every lesson, lesson PDF guides, quizzes and your certificate - and start creating your own pieces today.