Arrays
Master JavaScript arrays! Learn CRUD operations, iteration methods like map, filter, reduce, and array manipulation techniques.
Master JavaScript arrays! Learn CRUD operations, iteration methods like map, filter, reduce, and array manipulation techniques. This hands-on tutorial focuses on practical implementation of arrays concepts.
Arrays
Arrays are one of the most important data structures in JavaScript. They allow you to store multiple values in a single variable.
1. What are Arrays? π¦
What is it? An ordered list of values. Each value is called an "element" and has a numeric position called an "index".
Why use it? To store lists of data (e.g., a shopping list, user IDs, or colors) without creating separate variables for each item.
How it works:
Arrays are zero-indexed, meaning the first item is at index 0.
2. Basic Methods (Add/Remove) ββ
JavaScript provides built-in methods to add or remove items easily.
push(): Add to endpop(): Remove from endunshift(): Add to startshift(): Remove from start
3. Iterating Arrays π
How do you go through every item in an array?
forEach (Execute code for each item)
Great for side effects (printing, saving to DB).
map (Transform items)
Creates a new array by changing each item.
filter (Select items)
Creates a new array with only items that pass a test.
4. The reduce Method πͺ
What is it? The most powerful array method. It takes an array and "reduces" it to a single value (e.g., a sum, an object, or a number).
How it works: It loops through the array, maintaining an "accumulator" (running total).
5. Searching & Sorting π
find(): Returns the first item that matches.includes(): Returnstrueif item exists.sort(): Sorts the array (converts to string by default!).
Practical Example: Shopping Cart π
AI Mentor
Confused about "JavaScript arrays, push, pop, map, filter, reduce, and sorting"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 4Which method creates a NEW array with transformed elements?
Key Takeaways
β
Arrays are ordered lists (0-indexed).
β
push/pop add/remove from end.
β
map transforms, filter selects.
β
reduce combines everything into one value.
β
sort needs a compare function for numbers.
What's Next?
Arrays are powerful, but modern JavaScript gives us even better tools. Next, we'll learn Spread, Rest, and Destructuring!
Keep coding! π