HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
arr []push() / pop()shift() / unshift()indexOf() / includes()slice() / splice()join()map()filter()reduce()forEach()sort()
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›REFERENCE›Array
📋

Array

Working with ordered data collections and powerful array methods.

CommandDescription
arr []Collection of ordered values.
push() / pop()Adds to / removes from the end.
shift() / unshift()Removes from / adds to the beginning.
indexOf() / includes()Does the element exist / where is it?
slice() / splice()Extracts a slice / inserts-deletes in between.
join()Joins array elements into a single text.
map()Transforms each element and produces a new arr.
filter()Produces a new arr from elements that satisfy the condition.
reduce()Reduces the array to a single value.
forEach()Performs an action for each element (does not produce output).
sort()Sorts the array (in place).

arr []

Try it yourself →

Collection of ordered values.

Syntax

const name = [e1, e2, ...]

Description

An array is created with square brackets. Elements are accessed with a 0-based index.

Returns

Array.

Example

const colors = ["red", "purple"];
console.log(colors[0]);
console.log(colors.length);

Expected output

red
2

When to use?

Keeping multiple values (items, scores) in a single variable.

Common mistakes

  • ⚠️ Indexing starts at 0: the first element is arr[0].

push() / pop()

Try it yourself →

Adds to / removes from the end.

Syntax

arr.push(e) · arr.pop()

Description

push adds an element and returns the new length; pop removes the last element and returns it.

Example

const a = [1, 2];
a.push(3);
console.log(a);
console.log(a.pop());

Expected output

[ 1, 2, 3 ]
3

When to use?

Stack, growing lists, queue.

shift() / unshift()

Try it yourself →

Removes from / adds to the beginning.

Syntax

arr.shift() · arr.unshift(e)

Description

shift removes and returns the first element; unshift adds to the beginning.

Example

const a = [1, 2, 3];
console.log(a.shift());
a.unshift(0);
console.log(a);

Expected output

1
[ 0, 2, 3 ]

When to use?

Queue, processing from the front.

indexOf() / includes()

Try it yourself →

Does the element exist / where is it?

Syntax

arr.indexOf(e) · arr.includes(e)

Description

indexOf returns the position (or -1 if not found), includes returns true/false.

Example

const a = ["x", "y", "z"];
console.log(a.indexOf("y"));
console.log(a.includes("q"));

Expected output

1
false

When to use?

Checking if a value exists in the list.

slice() / splice()

Try it yourself →

Extracts a slice / inserts-deletes in between.

Syntax

arr.slice(b, s) · arr.splice(i, count)

Description

slice returns a new portion (does not mutate the original). splice inserts/deletes in place (modifies the original).

Example

const a = [1, 2, 3, 4];
console.log(a.slice(1, 3));
a.splice(0, 2);
console.log(a);

Expected output

[ 2, 3 ]
[ 3, 4 ]

When to use?

slice: copy/portion; splice: element removal/addition.

Common mistakes

  • ⚠️ slice preserves the original, splice MUTATES it — don't confuse them.

join()

Try it yourself →

Joins array elements into a single text.

Syntax

arr.join(separator)

Description

Combines elements by placing a separator between them (opposite of split).

Returns

Text (string).

Example

console.log(["2024", "10", "02"].join("-"));

Expected output

2024-10-02

When to use?

Converting an array into a single-line text (date, path, sentence).

map()

Try it yourself →

Transforms each element and produces a new arr.

Syntax

arr.map(element => newValue)

Description

Runs the function you provide for each element; creates a NEW arr from the results. Does not modify the original.

Returns

New arr.

Example

const numbers = [1, 2, 3];
console.log(numbers.map(x => x * 2));

Expected output

[ 2, 4, 6 ]

When to use?

Transforming a list into another list (adding tax to prices, capitalizing names).

filter()

Try it yourself →

Produces a new arr from elements that satisfy the condition.

Syntax

arr.filter(element => condition)

Description

Tests the condition for each element; includes those that return true in the new array.

Returns

New (filtered) arr.

Example

const numbers = [1, 2, 3, 4];
console.log(numbers.filter(x => x % 2 === 0));

Expected output

[ 2, 4 ]

When to use?

Selecting only desired items from a list (even numbers, active users).

reduce()

Try it yourself →

Reduces the array to a single value.

Syntax

arr.reduce((total, e) => ..., start)

Description

Accumulates elements into a single result (total, product, maximum...). The second argument is the initial value.

Returns

A single value.

Example

const numbers = [1, 2, 3, 4];
console.log(numbers.reduce((t, x) => t + x, 0));

Expected output

10

When to use?

Sum, average, cart total, count.

forEach()

Try it yourself →

Performs an action for each element (does not produce output).

Syntax

arr.forEach(element => { ... })

Description

Similar to map but does not return a new arr; just runs the job you provide for each element.

Example

["a", "b"].forEach(h => console.log(h));

Expected output

a
b

When to use?

Printing each element, side-effect operations.

Common mistakes

  • ⚠️ Use map instead of forEach if you need a new arr.

sort()

Try it yourself →

Sorts the array (in place).

Syntax

arr.sort((a, b) => a - b)

Description

Default sorting is as TEXT (10, 2 -> 10 comes first!). Provide a comparator for numbers: (a,b)=>a-b.

Example

const s = [10, 2, 33, 4];
s.sort((a, b) => a - b);
console.log(s);

Expected output

[ 2, 4, 10, 33 ]

When to use?

Leaderboard, alphabetical/numerical sorting.

Common mistakes

  • ⚠️ sort without a comparator sorts numbers like text — resulting in [1,10,2].
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code