JavaScript Arrays

Store lists of values.

✍️ 3 exercises + code challenge in this lesson

An array stores an ordered list of values in one variable: const fruits = ["Mango", "Apple", "Banana"];

  • Elements are accessed by index, starting at 0: fruits[0].
  • length gives the number of elements.
  • Arrays can hold any type, even other arrays and objects.
  • Array.isArray(x) checks whether something is an array.

Key Takeaways: JavaScript Arrays

Mastering JavaScript Arrays helps you write clean and reliable JavaScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

JavaScript Arrays Code Example

JavaScript
<pre id="out"></pre>
<script>
const log = (...a) => document.getElementById("out").textContent += a.join(" ") + "\n";
const fruits = ["Mango", "Apple", "Banana"];
log("First:", fruits[0]);
log("Last:", fruits[fruits.length - 1]);

fruits[1] = "Orange";
log("After change:", fruits);
log("Count:", fruits.length);

const mixed = [42, "text", true, { city: "Lahore" }, [1, 2]];
log("Mixed:", JSON.stringify(mixed));
log(Array.isArray(fruits));
</script>
Free sign-in required · Google login available

JavaScript Arrays Practice Exercises

Check what you learned in “JavaScript Arrays”.

/ 3 solved
Fill in the blank

Get the first element of the array.

const fruits = ["Mango", "Apple"];
log(fruits[  ]);
✓ Correct! Not quite. Try again.
Multiple choice

What is the index of the last element in an array of length 5?

✓ Correct! Not quite. Try again.
Code challenge

Create an array with "red", "green", "blue" and log how many items it has.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services