Catalog / JavaScript Cheatsheet
JavaScript Cheatsheet
A comprehensive cheat sheet covering essential JavaScript concepts, syntax, and best practices for developers of all levels.
JavaScript Fundamentals
Variables and Data Types
|
Oldest way to declare a variable. Function-scoped or globally-scoped. |
|
Block-scoped variable declaration. Preferred for variables that may be re-assigned. |
|
Block-scoped constant declaration. Value cannot be re-assigned after initialization. |
Primitive Data Types |
String, Number, Boolean, Null, Undefined, Symbol, BigInt |
Objects |
Collections of key-value pairs. Keys are strings, values can be any data type. |
Arrays |
Ordered lists of values. |
Operators
Arithmetic |
|
Assignment |
|
Comparison |
|
Logical |
|
Ternary |
|
typeof |
returns a string indicating the type of the unevaluated operand. |
Control Flow
|
|
|
|
|
|
|
Functions and Objects
Functions
Function Declaration |
|
Function Expression |
|
Arrow Function |
Implicit return:
|
|
Refers to the object it belongs to. Its value depends on how the function is called. |
Callback Functions |
Functions passed as arguments to other functions. |
Closures |
Functions that have access to variables from their outer scope, even after the outer function has finished executing. |
Objects
Object Literal |
|
Accessing Properties |
|
Adding/Modifying Properties |
|
Deleting Properties |
|
Object Constructor |
|
Classes |
|
Arrays
Creating Arrays:
|
Array Methods:
|
DOM Manipulation
Selecting Elements
|
Selects an element by its ID. |
|
Selects the first element that matches a CSS selector. |
|
Selects all elements that match a CSS selector (returns a NodeList). |
|
Selects all elements with a given class name (returns an HTMLCollection). |
|
Selects all elements with a given tag name (returns an HTMLCollection). |
Modifying Elements
|
Gets or sets the HTML content of an element. |
|
Gets or sets the text content of an element. |
|
Sets the value of an attribute on an element. |
|
Gets the value of an attribute on an element. |
|
Adds a class to an element. |
|
Removes a class from an element. |
|
Toggles a class on an element. |
Events
Adding Event Listeners:
Common Events:
|
Example:
|
Asynchronous JavaScript
Callbacks
Functions that are executed after another function has finished executing. Example:
|
Promises
Creating a Promise |
|
Handling a Promise |
|
Promise.all() |
|
Promise.race() |
|
Async/Await
Syntactic sugar for working with promises, making asynchronous code look and behave a bit more like synchronous code.
|