JavaScript Arrays and Objects Cheat Sheet
Array methods (map, filter, reduce, sort), objects, destructuring and spread, Map and Set, JSON and dates.
Updated
Creating and reading arrays#
const fruits = ["mango", "kiwi", "guava"];
fruits[0]; // "mango"
fruits.at(-1); // "guava"
fruits.length; // 3
Array.from({ length: 3 }, (_, i) => i * 2); // [0, 2, 4]
new Array(3).fill(0); // [0, 0, 0]
[...new Set([1, 1, 2])]; // [1, 2] (remove duplicates)Adding and removing (these change the array)#
Non-mutating versions: toSorted, toReversed, toSpliced, with(i, x), slice, concat, [...a, ...b].
Transforming arrays#
const nums = [5, 12, 8, 130, 44];
nums.map((n) => n * 2); // new array, same length
nums.filter((n) => n > 10); // items that pass
nums.reduce((sum, n) => sum + n, 0); // single value: 199
nums.find((n) => n > 10); // 12 (first match) or undefined
nums.findIndex((n) => n > 100); // 3
nums.some((n) => n > 100); // true
nums.every((n) => n > 0); // true
nums.includes(8); // true
nums.indexOf(44); // 4
nums.toSorted((a, b) => a - b); // numeric sort (never sort numbers without a compare function)
nums.slice(1, 3); // [12, 8]
[[1, 2], [3, [4]]].flat(Infinity); // [1, 2, 3, 4]
["a b", "c"].flatMap((s) => s.split(" ")); // ["a", "b", "c"]
nums.forEach((n, i) => console.log(i, n)); // side effects only, returns undefinedChaining:
const products = [{ name: "Pen", price: 20, inStock: true }, { name: "Bag", price: 1500, inStock: false }];
const names = products
.filter((p) => p.inStock)
.toSorted((a, b) => b.price - a.price)
.map((p) => p.name);Objects#
const user = { name: "Asha", age: 21, "home city": "Pune" };
user.name; // dot notation
user["home city"]; // brackets for special keys
const key = "age";
user[key]; // brackets for keys in variables
user.email = "a@x.com"; // add
delete user.age; // remove
"name" in user; // true
Object.hasOwn(user, "name");
const name = "Ravi", role = "admin";
const shorthand = { name, role, [key]: 30 }; // shorthand + computed key
Object.keys(user); Object.values(user); Object.entries(user);
Object.fromEntries([["a", 1], ["b", 2]]); // { a: 1, b: 2 }
Object.groupBy([{ c: "x" }, { c: "y" }], (o) => o.c);
Object.freeze(user);Destructuring and spread#
const [first, second = 0, ...rest] = [10, 20, 30, 40];
const { name: fullName, city = "Unknown", ...others } = { name: "Asha", age: 21 };
let a = 1, b = 2;
[a, b] = [b, a]; // swap
const merged = { ...{ theme: "light", size: 16 }, ...{ theme: "dark" } }; // later wins
const copy = [...[1, 2, 3]]; // shallow copy
const deep = structuredClone({ nested: { x: 1 } }); // deep copy
Math.max(...[3, 9, 4]); // spread into argumentsMap and Set#
const stock = new Map([["pens", 40]]);
stock.set("books", 12).get("pens"); // 40
stock.has("ink"); stock.delete("pens"); stock.size;
for (const [item, qty] of stock) console.log(item, qty);
const tags = new Set(["html", "css", "html"]); // Set(2) { "html", "css" }
tags.add("js"); tags.has("css"); tags.size;Use a Map for dynamic or non-string keys; a Set for unique values.
JSON#
const json = JSON.stringify({ name: "Asha", tags: ["a"] }); // '{"name":"Asha","tags":["a"]}'
JSON.stringify({ a: 1 }, null, 2); // pretty print
JSON.parse(json).tags[0]; // "a"
try { JSON.parse("{bad"); } catch { console.log("invalid JSON"); }Dates become strings; undefined, functions, Map and Set are lost.
Dates#
const d = new Date("2026-11-14T09:30:00Z"); // ISO string (Z = UTC)
d.getUTCFullYear(); d.getUTCMonth(); // month is 0-based
d.toISOString();
d.toLocaleDateString("en-IN", { dateStyle: "long", timeZone: "Asia/Kolkata" });
const days = Math.round((new Date("2026-12-01") - new Date("2026-11-01")) / 86_400_000); // 30Want the full explanations? Go to the Learn JavaScript course →