JavaScript Arrays: Basic Concepts and Usage Examples

JavaScript Arrays: Basic Concepts and Usage Examples

“When one variable isn’t enough… it’s time to use arrays.” 🚀

One day, while learning JavaScript, you realize something very important:

“I can’t survive with just one piece of data.”

One user isn’t enough.
One product isn’t enough.
One error message… is never enough 😅

And right at that moment, JavaScript hands you this gift:
🎁 Array


📦 What Is an Array? (Real-Life Version)

An array is simply putting multiple values into a single container.

Think about real life:

  • A shopping basket 🧺
  • A playlist 🎵
  • A WhatsApp group 💬 (the noisy one 😄)

JavaScript equivalent:

let shoppingList = ["Bread", "Milk", "Eggs", "Chocolate"];

What does this code do?

  • shoppingList → the name of the basket
  • [] → the sign that says “I’m an array”
  • Everything inside → elements (items)

💡 Tip:
An array can contain strings, numbers, booleans, objects, even other arrays.
JavaScript is pretty chill about this 😎


🛠️ Ways to Create an Array (Which One Should I Use?)

✅ 1. The Cleanest and Most Common Way

let numbers = [1, 2, 3, 4, 5];

  • Readable ✔️
  • Short ✔️
  • Everyone uses it ✔️

Golden rule:
Beginner + Professional = this method


⚠️ 2. Array Constructor (Know it, but rarely use it)

let numbers = new Array(1, 2, 3);

So where’s the problem?

let numbers = new Array(5);

Do you know what this does?
👉 It creates an empty array with 5 slots 😬

That’s why saying:

“I know this, but I don’t use it”
is the healthiest mindset.


🔍 Accessing Array Elements (The Index Issue)

Arrays are numbered, but there’s a twist:
👉 They start from 0.

let colors = ["Red", "Blue", "Green"];

IndexValue
0Red
1Blue
2Green

Usage:

console.log(colors[0]); // Red
console.log(colors[2]); // Green

🎯 Most common mistake:

colors[3]; // undefined

JavaScript says:

“That element doesn’t exist, sorry.” 😄


📏 Array Length (length = Life Saver)

let fruits = ["Apple", "Banana", "Strawberry"];
console.log(fruits.length); // 3

Why is this useful?

  • In loops
  • When accessing the last element
  • When making checks

Trick to get the last element 🔥

let lastFruit = fruits[fruits.length - 1];
console.log(lastFruit); // Strawberry

This single line can take you from junior to mid-level 😎


➕ Adding Elements to an Array (Fill the Basket)

🟢 Add to the end: push()

let cities = ["Istanbul", "Ankara"];
cities.push("Izmir");

console.log(cities);

📌 What happened?

  • "Izmir" was added to the end
  • push → “add from the back”

🟢 Add to the beginning: unshift()

cities.unshift("Bursa");

📌 This time:

  • The element went to the beginning
  • All indexes shifted (important!)

⚠️ unshift can affect performance in large arrays.


➖ Removing Elements from an Array (Diet Time 😄)

🔴 Remove from the end: pop()

let lastCity = cities.pop();
console.log(lastCity);

💡 pop returns the removed element
This is extremely useful!


🔴 Remove from the beginning: shift()

let firstCity = cities.shift();

shift + unshift
👉 shift indexes
👉 be careful with large arrays


🔁 Looping Through an Array (Meeting forEach)

let animals = ["Cat", "Dog", "Bird"];

animals.forEach(function(animal) {
  console.log(animal);
});

Let’s break this down:

  • forEach → “Do this for every element”
  • animal → the current element
  • console.log(animal) → print it

📌 Output:

Cat
Dog
Bird

forEach means:

“Walk through one by one, don’t interfere, just observe.” 😄


🧠 Practical Tips & Mini Tricks

✔️ Is it an array?

Array.isArray([]); // true
Array.isArray({}); // false


✔️ Copying an array (VERY IMPORTANT)

let original = [1, 2, 3];
let copy = [...original];

If you directly assign it:

let copy = original;

Both variables point to the same array 😱


✔️ Checking if an array is empty

if (fruits.length === 0) {
  console.log("The basket is empty");
}


🎯 Why Are Arrays So Important?

Because the real world works like this:

  • APIs → return arrays
  • User lists → arrays
  • Comments → arrays
  • Shopping carts → arrays

In JavaScript:

“I don’t know arrays”
=
“I’ll struggle in real projects”

But here’s the good news:
Anyone who understands arrays understands 60% of JavaScript 🚀


☕ Short but Powerful Summary

Arrays are the heart of real projects ❤️.

Array = multiple values

Index starts from 0

push / pop / shift / unshift are core tools

length is a hidden superpower

Comments

No comments yet. Why don’t you start the discussion?

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir