JavaScript Arrays 101

Search for a command to run...

No comments yet. Be the first to comment.
If you build React Native apps long enough, eventually you realize: navigation is not just “moving between screens”. It’s actually: moving between screens while preserving application state That soun
Most React Native apps look clean in the beginning. You create a few folders: /screens /components /hooks /utils everything feels organized… until the app grows. You add: authentication realtime sy
One of the most important concepts in Node.js is: The Event Loop It’s the reason Node.js can: handle thousands of requests process async operations efficiently remain responsive with a single JavaS
One of the biggest reasons Node.js became popular is its: Non-Blocking Architecture But to understand why that matters, we first need to understand the problem with: Blocking Code The difference betwe
Modern web applications constantly communicate with servers. Examples: frontend fetching user profiles mobile apps loading products dashboards updating analytics applications sending login request
When we write programs we often need to store multiple values.
For example imagine storing movie names.
let movieOne = "Inception"
let movieTwo = "Interstellar"
let movieThree = "Dune: Part Two"
This works for small data, but what if we need to store 50 movies or 100 student marks? Creating variables for each value would become messy.
This is where arrays help us.
Arrays allow us to store multiple values in a single memory reference and access them easily.
Arrays are non primitive values and are a special type of object in JavaScript.
Arrays allow us to store multiple values inside a single variable.
These values can be numbers, strings, objects or even other arrays.
There are many ways to create arrays in JavaScript.
The most common way is using square brackets.
[1, 2, 3, 4, 5]
Just write values between [] and your array is ready.
Example:
const numbers = [1, 2, 3, 4, 5]
Array(6)
This creates:
[ <6 empty items> ]
This creates an array with 6 empty slots.
Array.of(9)
This creates:
[9]
It creates an array containing the provided values. You can also add multiple values separated by commas.
Example:
Array.of(1, 2, 3)
Array.from("six")
This creates:
["s","i","x"]
It converts iterable data into an array.
Here every character of the string becomes an element in the array.
Arrays are 0 based. That means indexing starts from 0.
Example array:
const movies = ["Inception", "Interstellar", "Shawshank Redemption", "Dune: Part Two", "Se7en"]
Indexes look like this:
0 1 2 3 4
To access individual values we use:
variableName[index]
Example:
console.log(movies[0]) // Inception
console.log(movies[1]) // Interstellar
console.log(movies[2]) // Shawshank Redemption
console.log(movies[3]) // Dune: Part Two
console.log(movies[4]) // Se7en
We can easily update values inside an array by targeting the index.
Example:
movies[0] = "Forrest Gump"
Now print the array.
console.log(movies)
Output:
["Forrest Gump", "Interstellar", "Shawshank Redemption", "Dune: Part Two", "Se7en"]
So we accessed index 0 and updated its value.
Counting small arrays is easy.
You can just count 1 to 5.
But imagine counting elements of a very large array. That would be difficult.
JavaScript gives us a built in property called length.
Let's use the same movie array because these are some of the best movies.
const movies = ["Inception", "Interstellar", "Shawshank Redemption", "Dune: Part Two", "Se7en"]
console.log(movies.length) // 5
Important thing:
Length and index are not the same.
Length tells how many values exist
Index tells position of each value
Instead of accessing elements one by one, we can loop through the array.
Here I'm using a simple for loop to print every movie.
const movies = ["Inception", "Interstellar", "Shawshank Redemption", "Dune: Part Two", "Se7en"]
for(let i = 0; i < movies.length; i++){
console.log(movies[i])
}
Output:
Inception
Interstellar
Shawshank Redemption
Dune: Part Two
Se7en
This loop runs from index 0 to the last index and prints each value.
Arrays are one of the most commonly used data structures in JavaScript.
They help us store and manage multiple values easily.