JavaScript Cheat Sheet — FreeCodeCamp
Basics Use let to declare variables you may reassign. Use const for variables that shouldn’t change. Variable names: letters, numbers, _, $ — can’t start with a number. Initializat…

Table of Contents:
Basics
Use
letto declare variables you may reassign.Use
constfor variables that shouldn’t change.Variable names: letters, numbers,
_,$— can’t start with a number.Initialization = assigning value at declaration.
Uninitialized variables default to
undefined.
let name = "John"; // initialized
let age; // uninitialized → undefined
Data Types
Primitive Types (7):
string,number,boolean,null,undefined,symbol,bigintStrings are immutable (can't change characters, only reassign the whole value).
Non-Primitive Data Types
Objects →
{ name: "John" }Arrays →
[1, 2, 3]Functions →
function greet() {}Others →
Date,RegExp,Map,Set, etc.
These are reference types, not copied by value.
Console & Comments
console.log()— display output.Single-line:
// commentMulti-line:
/* comment */
Naming Convention
- Use camelCase for multi-word variables:
userName,totalCount
Math & Operators
Arithmetic:
+,-,*,/,%Increment:
x++, Decrement:x--PEMDAS order of operations applies.
Arrays
Arrays:
let arr = [1, 2, 3]Access:
arr[0](first element)Last item:
arr[arr.length - 1]Mutate:
arr[0] = 99.push()— add to end.pop()— remove from end and return itshift()— removes first item from arrayunshift()— adds item at the beginning of arrayrepeat()— repeats a string N times
Loops
// For loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// For...of loop
for (const val of arr) {
console.log(val);
}
- Off-by-one error: Watch index starts at 0.
while Loop
jsCopyEditlet i = 0;
while (i < 5) {
console.log(i);
i++;
}
Runs as long as the condition is true.
Don't forget to update the variable (
i++), or it may become an infinite loop
String Methods
"abc".repeat(3); // "abcabcabc"
Functions
function greet(name) {
return "Hello, " + name;
}
greet("Alex"); // Hello, Alex
functiondefines it.Call it using
greet().Use parameters to make it reusable.
Use
returnto pass back a value and stop function execution.Arguments are values passed in during the function call.
Scope
Global scope: declared outside any block/function.
Local (block) scope: declared inside function/loop — not accessible outside.
const globalVar = "hi";
function sayHi() {
const localVar = "bye";
return globalVar; // can access globalVar
}
console.log(sayHi());
console.log(localVar); // ❌ Error
Logic & Conditions
if (condition) {
// run if truthy
}
Truthy: most values (e.g.,
"text",1,[])Falsy:
false,0,"",null,undefined,NaN
Reusability Example
function getName() {
const name = "Camper";
return name;
}
const value = getName(); // use returned value
If anything is missing, please let me know in the comments — I’ll definitely add it to the cheat sheet.