Arrays help us store lists of things. Common operations let us add, remove, or find items easily.
0
0
Common array operations in Javascript
Introduction
You want to keep a list of your favorite movies.
You need to add new tasks to a to-do list.
You want to check if a name is in a list of friends.
You want to remove finished items from a shopping list.
You want to count how many times a word appears in a list.
Syntax
Javascript
class SimpleArray { constructor() { this.items = []; } add(item) { this.items.push(item); } remove(item) { const index = this.items.indexOf(item); if (index !== -1) { this.items.splice(index, 1); } } contains(item) { return this.items.includes(item); } size() { return this.items.length; } print() { console.log(this.items); } }
The push method adds an item to the end of the array.
The splice method removes items by index.
Examples
Adds one item to an empty array and prints it.
Javascript
const myArray = new SimpleArray(); myArray.add('apple'); myArray.print();
Trying to remove an item from an empty array does nothing.
Javascript
const myArray = new SimpleArray(); myArray.remove('apple'); myArray.print();
Removes the first item when there are multiple items.
Javascript
const myArray = new SimpleArray(); myArray.add('apple'); myArray.add('banana'); myArray.remove('apple'); myArray.print();
Checks if items are in the array.
Javascript
const myArray = new SimpleArray(); myArray.add('apple'); console.log(myArray.contains('apple')); console.log(myArray.contains('banana'));
Sample Program
This program shows how to add, check, and remove items from an array wrapped in a class. It prints the array before and after changes.
Javascript
class SimpleArray { constructor() { this.items = []; } add(item) { this.items.push(item); } remove(item) { const index = this.items.indexOf(item); if (index !== -1) { this.items.splice(index, 1); } } contains(item) { return this.items.includes(item); } size() { return this.items.length; } print() { console.log(this.items); } } const fruits = new SimpleArray(); console.log('Initial array:'); fruits.print(); fruits.add('apple'); fruits.add('banana'); fruits.add('cherry'); console.log('After adding items:'); fruits.print(); console.log('Contains banana?', fruits.contains('banana')); console.log('Contains grape?', fruits.contains('grape')); fruits.remove('banana'); console.log('After removing banana:'); fruits.print(); fruits.remove('grape'); console.log('After trying to remove grape (not in list):'); fruits.print();
OutputSuccess
Important Notes
Adding an item with push is fast, O(1) time.
Removing an item requires finding it first, so it takes O(n) time where n is the array size.
Common mistake: Trying to remove an item not in the array does nothing, so always check if it exists if needed.
Use contains to check presence before removing if you want to avoid silent failures.
Summary
Arrays store lists of items in order.
Use push to add items at the end.
Use indexOf and splice to remove items by value.
Use includes to check if an item is in the array.