Reference Material

JavaScript Cheat Sheet

Data types, comparison operators, array and string methods, loops, functions, and DOM basics — everything a beginner needs on one page. Bookmark it.

📋 8 sections 🎯 For beginners 🔄 Updated: 2026
Sections

Data Types

TypeExampletypeof
Numberconst n = 42;"number"
Stringconst s = "hi";"string"
Booleanconst b = true;"boolean"
Arrayconst a = [1,2,3];"object"
Objectconst o = {x:1};"object"
undefineda variable is declared but has no value"undefined"
nullconst x = null;"object"

Variables: let, const, var

KeywordCan be reassignedWhen to use
constNoDefault choice — for anything that won't change
letYesWhen the variable's value will definitely change
varYesOutdated, not used in new code

Comparison Operators

OperatorMeaning
===Strict equality (compares both value and type)
!==Strict inequality
==Loose equality (with type coercion, best avoided)
> >= < <=Greater than, greater than or equal, less than, less than or equal
&&Logical AND (both conditions true)
||Logical OR (at least one condition true)
!Logical NOT (inverts the value)

Array Methods

MethodWhat it doesExample
push()Adds an element to the endarr.push(5)
pop()Removes and returns the last elementarr.pop()
shift()Removes and returns the first elementarr.shift()
unshift()Adds an element to the startarr.unshift(0)
map()Creates a new array by applying a function to each elementarr.map(x => x * 2)
filter()Creates a new array with only the matching elementsarr.filter(x => x > 5)
forEach()Loops over every element, returns nothingarr.forEach(x => console.log(x))
find()Returns the first matching elementarr.find(x => x > 5)
includes()Checks whether a value exists in the arrayarr.includes(5)
slice()Returns part of an array without changing the originalarr.slice(1, 3)
splice()Removes/adds elements, changing the originalarr.splice(1, 2)
join()Joins an array into a string with a separatorarr.join(", ")
reduce()Reduces an array to a single value (like a sum)arr.reduce((s,x)=>s+x, 0)

String Methods

Method / propertyWhat it doesExample
lengthString length"hello".length → 5
toUpperCase()Converts to uppercase"hi".toUpperCase() → "HI"
toLowerCase()Converts to lowercase"HI".toLowerCase() → "hi"
slice()Returns part of a string"hello".slice(0,2) → "he"
split()Splits a string into an array by a separator"a,b".split(",") → ["a","b"]
includes()Checks if a string contains a substring"hello".includes("ell") → true
trim()Removes whitespace from both ends of a string" hi ".trim() → "hi"
replace()Replaces part of a string"hi".replace("h","H") → "Hi"

Conditionals & Loops

ConstructSyntax
if / elseif (x > 5) { ... } else { ... }
if / else if / elseif (x>90){...} else if (x>70){...} else {...}
switchswitch (x) { case 1: ...; break; default: ...; }
forfor (let i = 0; i < 10; i++) { ... }
whilewhile (condition) { ... }

Functions

KindSyntax
Regular functionfunction sum(a, b) { return a + b; }
Function expressionconst sum = function(a, b) { return a + b; };
Arrow functionconst sum = (a, b) => a + b;
Calling a functionsum(2, 3); // 5

DOM Basics

Method / propertyWhat it does
document.querySelector()Finds the first element matching a CSS selector
document.querySelectorAll()Finds all elements matching a CSS selector
element.innerHTMLReads or sets an element's HTML content
element.textContentReads or sets the text inside an element
element.addEventListener()Attaches an event handler (click, input, etc.)
element.classList.add() / remove() / toggle()Manages an element's CSS classes
element.valueThe value of an input field (input, select)
element.styleDirect control over an element's CSS styles

What's next

Want to go deeper?

All of these topics are covered in detail in the free JavaScript course — with practical exercises after every lesson.