Support Online
Skip to main content

JavaScript Arrays: Data Storage and Management

In JavaScript, Array allows us to store multiple data in a single variable in an organized manner.
In this guide, you will learn the basics of arrays, methods for adding and deleting elements, and navigation techniques in arrays.

🎯 What You Will Learn in This Guide

  • Array creation methods ([] and new Array())
  • Indexing and using length
  • Adding and removing elements (push, pop, unshift, splice)
  • Element replacement and update
  • Navigating arrays with loops (for, for...of)
  • Common mistakes and performance tips

1️⃣ Array Creation Methods

An array is one of the most common data structures in JavaScript.
You can create it in two ways:

📘 Array Literal (Preferred Method)

let sehirler = ["İstanbul", "Ankara", "İzmir"];

📗 Array Constructor

let sehirler = new Array("İstanbul", "Ankara", "İzmir");

Both methods give the same result, but the literal method is more reliable.


2️⃣ Indexing and Accessing Arrays

Indices in JavaScript arrays start from 0. So the first element is 0, the second is 1, the third is 2.

let sehirler = ["İstanbul", "Ankara", "İzmir"];
console.log(sehirler[0]); // İstanbul
console.log(sehirler[2]); // İzmir

🔢 String Length

console.log(sehirler.length); // 3

🔍 Finding the Index of a Specific Element

sehirler.indexOf("Ankara"); // 1

🧩 Getting the Last Element

sehirler[sehirler.length - 1]; // İzmir

3️⃣ Adding and Removing Elements to the Array

➕ push() — Append Element to End

sehirler.push("Trabzon");

➕ unshift() — Add First Element

sehirler.unshift("Bursa");

🗑 pop() — Delete Last Element

sehirler.pop();

🗑 shift() — Delete First Element

sehirler.shift();

🧰 splice() — Delete or Replace from a Specific Index

sehirler.splice(1, 1, "Adana"); // 1. indeksteki elemanı değiştirir

splice() can both delete elements and add new ones.


4️⃣ Updating Array Elements

There are two methods to replace elements:

Direct Assignment

sehirler[0] = "Konya";

Update with splice()

sehirler.splice(2, 1, "Gaziantep");

5️⃣ Traversing Arrays with Loops

🔁 for Loop

let yemekler = ["mantı", "kebap", "dolma"];
for (let i = 0; i < yemekler.length; i++) {
console.log(i, yemekler[i]);
}

🔁 for...of Loop

for (let yemek of yemekler) {
console.log(yemek);
}

for...of works only with values without dealing with index. Your code will be cleaner.


💬 Frequently Asked Questions (FAQ)

  1. Why do arrays start from 0?

The address of the first element in memory is represented by “0 offset”. This is a standard approach to performance.

  1. What is the difference between splice() and slice()?

splice() replaces the original array. slice(), on the other hand, does not touch the original array, only returns its copy.

  1. What happens if the index is omitted?

Empty elements (undefined) are created. Therefore it is safer to use push() or splice().

  1. What is the safest way to copy an array?

Use the spreading operator (...):

let kopya = [...sehirler];
  1. What is the difference between array and object?

Arrays for sequential data; objects are for key-value structure.


🏁 Conclusion

Arrays are the most efficient way to organize data in JavaScript. When you use indexing, push, pop and splice methods correctly, your code becomes both readable and powerful.

🚀 Test and publish the applications you develop on the GenixNode Platform on high-performance servers.