Limited Offer
60% OFF on all plans!

List / Array in Node.js

Complete and in-depth guide to JavaScript arrays (lists): creation, constructor behavior, dynamic typing, access, mutation, traversal, searching, joining, comparison, insertion, removal, transformations, cloning, flattening, deduplication, nested arrays, and all built-in higher order functions.

Array Creation & Constructor

Arrays in JavaScript can be created using literals or the Array constructor. Although both create arrays, the constructor has special behaviors that can easily cause bugs if not understood.

1. Array Literal

Create arrays using square brackets.

2. Array Constructor

Create arrays using the Array() constructor.

Array Fundamentals

JavaScript arrays are dynamic lists. Their size and contents can change at runtime without restrictions.

1. List of Dynamic Data Types

Arrays can store mixed data types.

2. Getting List Size

Use the length property.

3. Accessing List Element at Specific Index

Use array[index].

4. Updating List Element at Specific Index

Assign a new value using index.

5. Removing List Element at Specific Index

Use splice(index, count).

Traversing a List

Traversal is required when you want to process each element in a list.

1. Regular for Loop

Index-based traversal.

2. for...of Loop

Value-based traversal.

3. forEach (Higher Order Function)

Callback-based traversal.

Searching, Joining & Comparing

These operations are common in validation, display, and data comparison.

1. Check if an Element Exists

Use includes().

2. Find Element Index

Use indexOf().

3. Find Element Index from Last

Use lastIndexOf().

4. Joining Array Elements

Use join().

5. Comparing Arrays by Contents

Use JSON.stringify().

Insertion & Removal

JavaScript arrays provide multiple ways to insert and remove elements.

1. Insert/Remove from End

Use push() and pop().

2. Insert/Remove from Start

Use unshift() and shift().

3. Insert & Remove at Specific Index

Use splice().

Advanced Array Operations

Advanced operations help transform arrays efficiently.

1. Reverse an Array

Use reverse().

2. Clear an Array

Set length to 0.

3. Shuffle an Array

Use sort with Math.random.

4. Multi-dimensional Array

Nested arrays.

5. Traversing Multi-dimensional Arrays

Nested loops.

6. Concatenating Arrays

Use concat().

7. Flattening Arrays

Use flat().

8. Removing Duplicates

Use Set.

9. Shallow & Deep Clone

Spread, JSON, structuredClone.

Higher Order Functions

These functions operate on arrays without manual loops.

1. map

Transform values.

2. filter

Select values.

3. find

Find first match.

4. findIndex

Find index of match.

5. every

All elements must match.

6. some

At least one matches.

7. sort (numbers, strings, dates)

Sorting arrays.

8. reduce

Aggregate values.