JavaScriptArrays & Objects
JavaScript Arrays
Learn JavaScript arrays, how to create them, access elements, modify values, and use arrays effectively to manage multiple data items.
What is an Array in JavaScript?Link to this section
An array is a data structure used to store multiple values in a single variable. Instead of creating separate variables for related data, arrays allow grouping values together in an ordered list.
Arrays are widely used to store collections such as user lists, product data, scores, and more.
Why Use Arrays?Link to this section
Arrays help:
- Manage large amounts of related data
- Reduce code repetition
- Improve data organization
- Enable efficient data processing
Creating an Array :Link to this section
Arrays are created using square brackets [].
note
Array elements can be of different data types.
Accessing Array Elements :Link to this section
Array elements are accessed using their index number, starting from 0.
Explanation:
The first element is always at index 0, not 1.
Modifying Array Elements :Link to this section
You can update an array element by assigning a new value to a specific index.
Array Length :Link to this section
The length property returns the total number of elements in an array.
tip
Array length is often used in loops to iterate through elements safely.
Common Array Methods :Link to this section
push()
Adds an element to the end of an array.pop()
Removes the last element from an array.shift()
Removes the first element from an array.unshift()
Adds an element to the beginning of an array.Looping Through Arrays :Link to this section
Arrays are commonly processed using loops.
Arrays in Real-World Applications :Link to this section
rrays are used for:
- Displaying lists (users, products, posts)
- Processing API responses
- Managing form inputs
- Handling game scores or rankings
tip
Mastering arrays is essential before learning advanced concepts like array methods (map, filter, reduce).
Check Your Understanding
Question 1 of 4What is the index of the first element in an array?
Create an Array
Create an array containing five of your favorite movies..
Access Elements
Print the first and last elements of an array..