JavaScript Array
JS
In JavaScript, an Array is used to store multiple values in a single variable. You can think of it as a list or collection of items.
Creating an Array:
Accessing Array Elements:
Array elements have index numbers starting at 0:
Adding Elements:
Use methods like push() and unshift():
Removing Elements:
Use methods like pop() and shift():
Looping Through an Array:
Using a loop to display all items:
Using Array Methods:
- forEach() (Iterates through elements):
- map() (Transforms each element into a new array):
- filter() (Creates new array based on condition):
Array of Objects:
Arrays can store objects, which is useful for real-world data:
Benefits:
- Store multiple items efficiently.
- Easy to access, add, and remove items.
- Great for looping and handling lists of data.
- Provide many built-in powerful methods (map, filter, reduce, etc.).
Array Projects
To-Do List Application
Features:
- Add new tasks to the list
- Remove tasks by clicking a delete button
- Mark tasks as completed by clicking on them
- Shows clear usage of JavaScript arrays, DOM manipulation, and event handling.
Project Code: