December 3, 2026 - 08:49
JavaScript Lesson 08 — JavaScript Arrays: Store More Than One Value Image
JavaScript Tutorials

JavaScript Lesson 08 — JavaScript Arrays: Store More Than One Value

0 Comments

We could declare topic1, topic2 and topic3 separately. A fourth topic would need another variable. But counting the topics or processing all of them becomes awkward. An array keeps related values together.

The first position is zero

JAVASCRIPT
const topics = ['HTML', 'CSS', 'JavaScript'];
console.log(topics[0]);
console.log(topics.length);
topics.push('Linux');
console.log(topics.length);
console.log(topics.includes('PHP'));
console.log(topics[10]);

Square brackets create the array and commas separate its values. topics[0] returns HTML. Arrays are zero-indexed: the first position is 0, the second 1 and the third 2. Three elements therefore end at index 2.

topics.length initially returns 3. Length is a property, so we do not add call parentheses. Push is a method: an operation called through the array. It appends Linux, after which the length is 4.

topics.includes('PHP') returns false for this list. Includes checks whether a value is present. Try changing the case of HTML to see that string matching is case-sensitive.

An out-of-range access may be quiet

topics[10] produces undefined in this example rather than throwing an obvious bounds error. A wrong index can therefore cause trouble later when its result is used elsewhere. Keep the length and the last valid index distinct.

A const binding does not freeze the contents

We cannot assign a different array to the topics binding, but we can modify the existing array. topics = [] would be a forbidden reassignment. topics.push('Linux') changes the current array and is allowed.

Push does not return the new array

JAVASCRIPT
const topics = ['HTML', 'CSS'];
const newLength = topics.push('Linux');
console.log(newLength);
console.log(topics[2]);
const removed = topics.pop();
console.log(removed);
console.log(topics.length);

The results are 3, Linux, Linux and 2. Push mutates the array and returns its new length. Pop removes the final item and returns that item. Separate an operation's effect from its return value.

Popping an empty array returns undefined. Do not assume every input list contains the three convenient items from the first example.

Removing a middle item shifts indexes

JAVASCRIPT
const topics = ['HTML', 'CSS', 'Linux'];
const removed = topics.splice(1, 1);
console.log(removed[0]);
console.log(topics[1]);
console.log(topics.length);

Splice starts at index one and removes one item. CSS leaves, Linux shifts to index one, and the method returns an array of removed items. A position is not a permanent record identifier.

Remember zero-based indexing: index one was the second element. We will distinguish stable record identifiers when working with objects later.

Slice is a different operation

JAVASCRIPT
const original = ['HTML', 'CSS', 'Linux'];
const part = original.slice(0, 2);
console.log(part.length);
console.log(original.length);
part.push('JavaScript');
console.log(original.length);

The outputs are 2, 3 and 3. Slice returns a new array without removing those items from the source. Its end index is excluded. Unlike splice, this call does not mutate the original.

The copy is shallow. We deliberately use strings here; nested object references require a further distinction. A new array does not imply an unlimited deep copy.

Length is not the last index

Three elements have length three and final index two. An empty array gives length minus one as -1, which is not ordinary last-element indexing. Do not import negative-index assumptions from another language.

Repeat the operations with zero, one and three items. Record both the array's state and each method's return value. Correct-looking text alone does not show which structure changed.

Assignment does not make a copy

JAVASCRIPT
const original = ['HTML', 'CSS'];
const sameList = original;
sameList.push('Linux');
console.log(original.length);

The result is 3. Both names refer to the same array; sameList is not an independent copy. Adding through one name is visible through the other. We will explore deliberate copying separately.

Add a topic of your own, inspect the new length, and read the final element with topics[topics.length - 1]. This derives the index from the current size rather than guessing it.

References: MDN

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment