TechSchool
Home / Cheat sheets / JavaScript

JavaScript cheat sheet

96 one-line examples for JavaScript. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.

Variables and Types

CodeWhat it does
let x = 5;Declare a variable that you can change.
const y = 10;Declare a value that cannot change.
var z = 1;Old style variable. Prefer let or const.
let name = "Ana";A string holds text inside quotes.
let ok = true;A boolean is true or false.
let n = null;Null means no value on purpose.
let u;An empty variable has value undefined.
typeof 5Return the type name as a string.
let a = 1, b = 2;Declare two variables on one line.
x = 7;Assign a new value to a variable.
let big = 10n;A BigInt holds very large integers.
let s = Symbol();A symbol is a unique identifier.

Operators

CodeWhat it does
a + bAdd two numbers or join two strings.
a - bSubtract the right number from the left.
a * bMultiply two numbers.
a / bDivide the left number by the right.
a % bReturn the remainder of a division.
a ** 2Raise a number to a power.
a === bCheck value and type are equal.
a !== bCheck value or type is not equal.
a > bCheck if the left value is larger.
a && bTrue when both sides are true.
a || bTrue when one side is true.
!aReverse a boolean value.

Strings

CodeWhat it does
s.lengthReturn the number of characters.
s.toUpperCase()Return the string in capital letters.
s.toLowerCase()Return the string in small letters.
s.trim()Remove spaces from both ends.
s.includes("ab")Check if the text contains a part.
s.indexOf("a")Return the position of the first match.
s.slice(0, 3)Copy a part of the string.
s.replace("a", "b")Replace the first match with new text.
s.split(",")Split the string into an array.
s.repeat(3)Return the string copied three times.
s.charAt(0)Return the character at a position.
`Hi ${name}`A template string inserts a variable.

Arrays

CodeWhat it does
let a = [1, 2, 3];Create an array with three numbers.
a.push(4)Add an item to the end.
a.pop()Remove and return the last item.
a.shift()Remove and return the first item.
a.unshift(0)Add an item to the start.
a.lengthReturn the number of items.
a[0]Read the item at index zero.
a.indexOf(2)Return the position of an item.
a.join("-")Join all items into one string.
a.slice(1, 3)Copy a part of the array.
a.includes(2)Check if the array holds an item.
a.reverse()Reverse the order of the items.

Objects

CodeWhat it does
let o = {a: 1};Create an object with one property.
o.aRead a property with dot notation.
o["a"]Read a property with bracket notation.
o.b = 2;Add or change a property.
delete o.b;Remove a property from the object.
"a" in oCheck if a property exists.
Object.keys(o)Return an array of property names.
Object.values(o)Return an array of property values.
Object.entries(o)Return an array of key and value pairs.
let {a} = o;Copy a property into a variable.
let c = {...o};Make a shallow copy of the object.
Object.assign({}, o)Copy properties into a new object.

Functions

CodeWhat it does
function f(a) { return a; }Declare a named function with one parameter.
const f = (a) => a;An arrow function returns the value.
f(3)Call a function with one argument.
function f(a = 1) {}Set a default value for a parameter.
function f(...r) {}Collect extra arguments into an array.
return;Stop the function and return nothing.
const f = function () {};Store a function in a variable.
(() => 1)()Call an arrow function immediately.
f.call(null, 3)Call a function with a set this value.
f.apply(null, [3])Call a function with arguments in an array.
f.bind(null)Make a new function with a fixed this.
typeof f === "function"Check that a value is a function.

Control Flow

CodeWhat it does
if (a > 1) {}Run a block when the test is true.
else {}Run a block when the test is false.
a > 1 ? x : yPick one value with a short test.
switch (a) { case 1: }Compare one value against many cases.
break;Leave the switch or loop now.
for (let i = 0; i < 3; i++) {}Repeat a block a set number of times.
while (a < 3) {}Repeat a block while a test is true.
do {} while (a < 3);Run the block once, then test.
for (const k in o) {}Loop over the keys of an object.
for (const v of a) {}Loop over the values of an array.
continue;Skip to the next loop step.
throw new Error("bad");Stop the code and raise an error.

Array Methods

CodeWhat it does
a.map(x => x * 2)Make a new array with changed items.
a.filter(x => x > 1)Make a new array with matching items.
a.reduce((s, x) => s + x, 0)Combine all items into one value.
a.find(x => x > 1)Return the first matching item.
a.findIndex(x => x > 1)Return the position of the first match.
a.some(x => x > 1)Check if any item matches.
a.every(x => x > 1)Check if all items match.
a.sort((x, y) => x - y)Sort the array with a compare function.
a.forEach(x => log(x))Run a function for each item.
a.concat([4])Join two arrays into a new one.
a.flat()Flatten nested arrays by one level.
Array.isArray(a)Check that a value is an array.
More on TechSchool
See the JavaScript interview questions Python TypeScript HTML CSS React Vue 3 Node.js Java PHP C C++ C# Go SQL MongoDB Redis Linux Git Docker Data Structures