ToolHop.

ADVERT

๐Ÿง  JavaScript Cheatsheet

Modern JavaScript reference packed with syntax basics, array helpers, async patterns, modules, and DOM tips.

Interactive JavaScript cheat sheet with searchable snippets, one-click copy, and a local code playground for quick validation.

JS Playground

Run snippets locally in-browser. Return a value to inspect output.

Language Basics

Declarations, functions, and object/array ergonomics.

SnippetWhat it doesWhy it mattersCopy
const answer = 42;Block-scoped constantDefault to const and switch to let only when reassignment is required.
let total = 0;Mutable bindingKeep mutable scope tight to reduce side-effects.
const sum = (a, b) => a + b;Arrow functionLexical this and concise expressions for callbacks.
const { id, name } = user;DestructuringPull only required fields to keep code explicit.

Array Utilities

Transform, filter, and summarize collections.

SnippetWhat it doesWhy it mattersCopy
items.map((item) => item.id)ProjectionCreates a new array without mutating source values.
items.filter((item) => item.active)SelectionUse for UI filtering, guards, and feature flags.
items.reduce((sum, n) => sum + n, 0)AccumulationBuild aggregates, maps, and grouped results.
[...new Set(items)]DeduplicationFast unique list generation for primitives.

Async Patterns

Promise workflows and resilient fetch handling.

SnippetWhat it doesWhy it mattersCopy
const data = await fetch(url).then((r) => r.json());Async data loadingWrap in try/catch for robust error handling.
const results = await Promise.all(tasks);Parallel tasksUse allSettled when partial failures are acceptable.
const controller = new AbortController();Request cancelationAbort stale requests in interactive UIs.
try { ... } catch (error) { ... }Error boundariesCapture context before rethrowing or surfacing messages.

DOM and Events

Vanilla browser interactions for fast interfaces.

SnippetWhat it doesWhy it mattersCopy
document.querySelector('[data-role="button"]')Element selectionPrefer stable selectors over brittle class-name chains.
button.addEventListener('click', handleClick)Event bindingRemove listeners during cleanup in dynamic screens.
requestAnimationFrame(render)Smooth frame updatesUse for animation and layout-sensitive updates.
element.dataset.state = 'open'Dataset APIUseful for stateful CSS hooks and instrumentation.

ADVERT

ADVERT