Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays 101

Updated
•3 min read•View as Markdown
JavaScript Arrays 101
Y
Hi, I'm Yashika, a Software Engineer passionate about understanding how software works beneath the surface. I write practical, beginner-friendly deep dives on web development, TypeScript, Git, backend development, system design, databases, and modern software engineering. My goal is to simplify complex concepts through real-world examples, diagrams, and hands-on exploration.

What Are Arrays and Why Do We Need Them?

Imagine you want to store your 5 favorite movies.

Without arrays, you might do something like this:

const movie1 = "Inception";
const movie2 = "Interstellar";
const movie3 = "Avatar";
const movie4 = "Titanic";
const movie5 = "Joker";

This works, but it quickly becomes messy and hard to manage.

Instead, JavaScript gives us arrays, which allow us to store multiple values inside a single variable. In Js, you can create any type of value: string, boolean, number unli

const movies = ["Inception", "Interstellar", "Avatar", "Titanic", "Joker"];

Now all the values are organized in one place.

An array is a collection of values stored in order.


How to Create an Array

Arrays are created using square brackets [].

const movies = ["Inception", "Interstellar", "Avatar"];

You can store any type of value inside arrays.

const mixedArray = ["Rahul", 25, true];

Accessing Elements Using Index

Each item in an array has a position number called an index.

Important rule:

Array indexing starts from 0

const movies = ["Inception", "Interstellar", "Avatar", "Titanic", "Joker"];

console.log(movies[0]); // Inception
console.log(movies[4]); // Joker

So the structure looks like this:

Index →   0            1             2         3        4
Value → Inception  Interstellar   Avatar   Titanic   Joker

Updating Elements in an Array

We can update any value using its index.

Suppose you want to change Avatar to The Dark Knight.

const movies = ["Inception", "Interstellar", "Avatar", "Titanic", "Joker"];

movies[2] = "The Dark Knight";

console.log(movies);

Output:

["Inception", "Interstellar", "The Dark Knight", "Titanic", "Joker"]

Array Length Property

Arrays have a built-in property called length that tells us how many elements are inside the array.

const movies = ["Inception", "Interstellar", "Avatar", "Titanic", "Joker"];

console.log(movies.length);

Output:

5

This is useful when we want to access the last element.

console.log(movies[movies.length - 1]);

Output:

Joker

Looping Through an Array

Instead of printing each movie manually, we can use a loop to go through all values.

const movies = ["Inception", "Interstellar", "Avatar", "Titanic", "Joker"];

for (let i = 0; i < movies.length; i++) {
  console.log(movies[i]);
}

Output:

Inception
Interstellar
Avatar
Titanic
Joker

This loop runs through the entire array and prints every element.

JavaScript Deep Dive: Basics to Advanced

Part 5 of 12

A structured journey from JavaScript basics to advanced concepts, explained with real-world examples and deep understanding. This series focuses on not just "what" but "why" things work in JavaScript — covering core fundamentals, tricky concepts like closures and this keyword, async behavior, and beyond. Perfect for developers who want to move from confusion to clarity and actually master JavaScript.

Up next

Understanding Object-Oriented Programming in JavaScript

What Object-Oriented Programming (OOP) means? When programs start growing, managing data and behavior separately can become messy.This is where Object-Oriented Programming (OOP) helps. OOP is a progra

More from this blog

Yashika’s Dev Journey

29 posts

Documenting my journey from frontend to full stack development.

I write about JavaScript, backend concepts, and real-world development problems — focusing on understanding how things actually work rather than just using them.

Building in public, learning deeply, and sharing everything along the way.