Cheat Sheet

Use this as a reference while working.

Table of Contents:

Data Types and Variables

Data Types

Primitive Types (immutable, stored directly in memory):

Type
Example
Description

String

"hello", 'world'

Sequence of characters

Number

42, 10.5, -2

Numeric values

Boolean

true, false

Truth values

Undefined

undefined

Variable declared but not assigned

Null

null

Intentional absence of value

Reference Types (mutable, stored in heap memory):

Type
Example
Description

Object

{ name: "Alex", age: 25 }

Collection of key:value pairs

Array

["apple", "banana", "cherry"]

Ordered list of values

Function

(x) => x * 2

Reusable block of code

Type Coercion:

Use typeof to check a value's type:

Operators

Category
Operators
Notes

Arithmetic

+, -, *, /, % (modulus), ** (exponent)

Comparison

>, <, >=, <=

Equality

=== (strict), !== (strict not equal)

Always use strict equality, never ==/!=

Logical

&& (AND), || (OR), ! (NOT)

Ternary

condition ? valueIfTrue : valueIfFalse

Shorthand for if/else

Variables and Scope

Three operations you can perform with variables:

  1. Declare — create a new variable with let or const

  2. Assign — give the variable a value (only with let)

  3. Reference — use the variable to access its value

Scope determines where a variable can be accessed:

  • Block scope — inside {} of if/for/while blocks

  • Function scope — inside a function

  • Module scope — within a file

  • Global scope — accessible everywhere (avoid!)

Functions

Defining Functions

Arrow functions are preferred:

Parameters, Arguments, and Return Values

  • Parameters — placeholders in the function definition

  • Arguments — actual values passed when calling the function

  • Return values — the value sent back to the caller; terminates the function

Function calls resolve from the inside out:

Strings

String Basics

  • Each character has an index starting at 0

  • Strings are immutable — string methods return new strings

Template Literals (string interpolation):

Escape Characters: \" (quote), \n (newline), \t (tab)

String Methods

Read-only methods (return booleans or numbers):

Method
Returns
Description

.includes(str)

boolean

Does the string contain str?

.startsWith(str)

boolean

Does the string start with str?

.endsWith(str)

boolean

Does the string end with str?

.indexOf(str)

number

Index of first match (-1 if not found)

Methods that return a modified copy:

Method
Description

.slice(start, end)

Extract a portion of the string

.toUpperCase()

Convert to uppercase

.toLowerCase()

Convert to lowercase

.repeat(n)

Repeat string n times

.replace(str, replacement)

Replace first occurrence

.replaceAll(str, replacement)

Replace all occurrences

Conditional Statements

Order matters! Only the first true condition executes.

Guard Clauses — return early to simplify logic:

Ternary Operator — shorthand for simple if/else:

Node Modules and Testing

Modules

Modules let you split code across files and share functionality.

Exporting:

Importing:

npm and package.json

  • npm i package-name — install a package

  • npm i --save-dev jest — install as a dev dependency

  • package.json — lists dependencies and scripts

  • node_modules/ — contains downloaded packages (don't commit this!)

npm scripts in package.json:

Run with: npm test, npm run test:w, npm run dev

Jest Testing

Loops

For Loop — use when you know how many iterations:

While Loop — use when the number of iterations is unknown:

break vs return:

  • break — exits the current loop, continues after the loop

  • return — exits the loop AND the current function

Nested Loops:

Arrays

Array Basics

Mutating Arrays

Unlike strings, arrays are mutable:

Reference Types and Pure Functions

Reference type variables store a reference to data in memory, not the data itself:

Pure functions avoid mutating input values. Use the spread operator to copy:

Destructuring Arrays

Objects

Object Basics

Objects store related data as key:value pairs (properties):

Accessing properties:

Modifying Objects

Objects are reference types — use spread to copy:

Object shorthand — when key names match variable names:

Iterating Over Objects

Destructuring Objects

Error Handling

Common error types:

Error Type
Cause

SyntaxError

Invalid syntax (missing quotes, brackets)

ReferenceError

Variable not defined

TypeError

Wrong data type or method doesn't exist

RangeError

Value outside acceptable range

Reading an error message: look for the error type, the description, and the call stack (which shows the chain of function calls that led to the error).

Try/Catch — handle errors without crashing:

Higher-Order Functions and Callbacks

  • Higher-Order Function (HOF) — a function that accepts another function as an argument or returns a function

  • Callback — a function passed as an argument to a HOF

Important: Pass the function reference, don't invoke it:

Array.forEach — iterate and perform side effects:

Array Higher-Order Methods

Method
Callback Returns
Method Returns
Use Case

forEach(fn)

nothing

undefined

Perform side effects

map(fn)

transformed value

new array (same length)

Transform each element

filter(fn)

boolean

new array (subset)

Keep elements that pass

find(fn)

boolean

first matching element

Get first match

findIndex(fn)

boolean

index of first match

Get index of first match

reduce(fn, init)

accumulated value

single value

Combine into one value

sort(fn)

comparison number

sorted array (mutates!)

Sort elements

map, filter, find, findIndex

reduce

sort

Warning: .sort() mutates the original array!

Method Chaining

Regular Expressions (Bonus)

Regular expressions are patterns for searching, matching, and manipulating text.

Common patterns:

Pattern
Meaning

^

Start of string

$

End of string

\w

Word character

\d

Digit (0-9)

\s

Whitespace

.

Any character

+

One or more

*

Zero or more

?

Zero or one

[abc]

Character class

[^abc]

Negated class

{n}

Exactly n times

{n,m}

Between n and m

Flags: i (case insensitive), g (global — find all matches)

Methods:

Last updated