Cheat Sheet
Use this as a reference while working.
Table of Contents:
DOM (Document Object Model)
The DOM is a JavaScript representation of your HTML. It lets you select, modify, create, and delete elements on the page.
Linking JavaScript to HTML
Add a <script> tag at the end of your <body>:
Snippet: Load your JavaScript file after the HTML so the DOM is already available.
For ES6 modules (import/export), add type="module":
Snippet: Enable import / export syntax in browser JavaScript.
Note: Modules require a server (use the VS Code Live Server extension).
Selecting Elements
Use document.querySelector() with CSS selector syntax:
Snippet: Select one element using CSS selectors.
Use querySelectorAll() to get ALL matching elements (returns a NodeList):
Snippet: Select multiple elements and loop through them.
Modifying Elements
Snippet: Update text, styles, classes, and attributes on an existing element.
Creating Elements
The pattern: Create → Modify → Append
Snippet: Create a new element, customize it, and append it to the page.
Removing Elements
Snippet: Remove an element from the DOM.
Event Listeners
Listen for user interactions and run code when they happen:
Snippet: Handle a button click and inspect the event target.
click
Element is clicked
mouseover
Mouse enters an element
mouseout
Mouse leaves an element
keydown
A key is pressed
keyup
A key is released
submit
A form is submitted
input
An input's value changes
The event object is passed to your handler and contains useful info:
event.target— the element that triggered the eventevent.preventDefault()— stops default behavior (like form submission)
Handling Form Submissions
Snippet: Prevent default submit behavior, read form values, and reset the form.
Alternative: Access inputs directly via form.elements:
Snippet: Read specific input values by name from form.elements.
Common DOM Patterns
Render a list from an array:
Snippet: Convert array items into <li> elements and append them to a <ul>.
Toggle visibility:
Snippet: Toggle a CSS class to show/hide UI.
Servers, APIs, and HTTP
Client, Server, and API Basics
Client: your frontend app running in the browser
Server: a program on another computer that receives requests and sends responses
API (Application Programming Interface): an interface that lets apps request or send data
Endpoints
An endpoint is a specific URL for a specific resource/action.
https://dog.ceo/api/breeds/image/random-> one random dog imagehttps://jsonplaceholder.typicode.com/users-> a collection of usershttps://jsonplaceholder.typicode.com/users/1-> one specific user
Snippet: Send a GET request to an endpoint and handle the parsed response.
Note: this snippet does NOT include any error checking or handling.
HTTP Methods / Verbs
GET
Read data
POST
Create/send new data
PATCH / PUT
Update existing data
DELETE
Remove data
Snippet: Configure a POST request with a JSON body.
Status Codes
Status codes describe how the server handled your request:
2xx-> success (200 OK,201 Created)4xx-> client/request problem (400 Bad Request,401 Unauthorized,404 Not Found)5xx-> server problem (500 Internal Server Error)
In fetch code, inspect response.status and response.ok before you parse the body.
Snippet: Log status information from the Response object.
Async
Asynchronous code starts a process that takes time and does not block the rest of your program.
Key Concepts
Promiseobjects can bepending,resolved, orrejectedfetch(url, config)returns a Promise that resolves to aResponseUse
.then()/.catch()orasync/awaitwithtry/catch
fetch() 6-Step Pattern (.then() / .catch())
fetch() 6-Step Pattern (.then() / .catch())Snippet: Full fetch flow: (1) request, (2) handle the promise, (2) validate response, (3) parse JSON, (4) use data, (5) handle errors.
async / await Version
async / await VersionSnippet: Same fetch flow using async / await and standardized { data, error } returns.
Last updated