TypeScript Cheatsheet
48 essential methods for coding interviews
Showing 48 of 48 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
push / pop | arr.push(...items): number | arr.pop(): T | undefined | Add to end / remove from end (stack operations) | O(1) | essential |
shift / unshift | arr.shift(): T | undefined | arr.unshift(...items): number | Remove from start / add to start | O(n) | essential |
slice | array.slice(start?, end?): T[] | Returns shallow copy of portion of array | O(n) | essential |
splice | array.splice(start, deleteCount?, ...items): T[] | Removes/replaces/adds elements in place | O(n) | essential |
indexOf / includes | arr.indexOf(el): number | arr.includes(el): boolean | Find index of element / check if element exists | O(n) | essential |
find / findIndex | arr.find(predicate): T | undefined | arr.findIndex(predicate): number | Find first element/index satisfying predicate | O(n) | essential |
reverse | array.reverse(): T[] | Reverses array in place | O(n) | essential |
fill | array.fill(value, start?, end?): T[] | Fills array with static value | O(n) | essential |
Array.from | Array.from(iterable, mapFn?): T[] | Creates array from iterable with optional mapping | O(n) | essential |
join | array.join(separator?): string | Joins array elements into string | O(n) | essential |
map | array.map(callback): U[] | Creates new array with results of calling function on every element | O(n) | essential |
filter | array.filter(predicate): T[] | Creates new array with elements passing the test | O(n) | essential |
reduce | array.reduce(callback, initialValue?): U | Reduces array to single value by applying function | O(n) | essential |
forEach | array.forEach(callback): void | Executes function for each element | O(n) | essential |
some / every | arr.some(predicate): boolean | arr.every(predicate): boolean | Test if any/all elements pass predicate | O(n) | essential |
sort | array.sort(compareFn?): T[] | Sorts array in place | O(n log n) | essential |
split | string.split(separator, limit?): string[] | Splits string into array by separator | O(n) | essential |
substring / slice | str.substring(start, end?) | str.slice(start, end?) | Extract part of string | O(n) | essential |
charCodeAt / fromCharCode | str.charCodeAt(i): number | String.fromCharCode(...codes): string | Convert between characters and ASCII/Unicode codes | O(1) / O(n) | essential |
toLowerCase / toUpperCase | str.toLowerCase(): string | str.toUpperCase(): string | Convert string case | O(n) | essential |
Template literals | `string $expression` | String interpolation with embedded expressions using backticks | O(n) | essential |
Map basics | map.set(k, v) | map.get(k) | map.has(k) | map.delete(k) | Key-value store with any type as key | O(1) for all | essential |
Map iteration | map.keys() | map.values() | map.entries() | map.forEach() | Iterate over Map contents | O(n) | essential |
Set basics | set.add(v) | set.has(v) | set.delete(v) | set.size | Collection of unique values | O(1) for add/has/delete | essential |
Set conversion | [...new Set(arr)] | Array.from(set) | Convert between Set and Array | O(n) | essential |
Object.keys / values / entries | Object.keys(obj) | Object.values(obj) | Object.entries(obj) | Get arrays of keys, values, or [key, value] pairs | O(n) | essential |
Optional chaining | obj?.prop | obj?.[expr] | func?.() | Safely access nested properties | O(1) | essential |
Nullish coalescing | value ?? defaultValue | Returns right side if left is null/undefined | O(1) | essential |
Spread operator | [...arr] | {...obj} | Spreads iterable elements or object properties | O(n) | essential |
Destructuring | const { a, b } = obj; const [x, y] = arr; | Extract values from objects/arrays into variables | O(1) | essential |
Math.max / Math.min | Math.max(...values) | Math.min(...values) | Returns largest/smallest of given numbers | O(n) | essential |
Math.floor / ceil / round | Math.floor(x) | Math.ceil(x) | Math.round(x) | Round down / up / to nearest integer | O(1) | essential |
Math.abs / sqrt / pow | Math.abs(x) | Math.sqrt(x) | Math.pow(base, exp) | Absolute value / square root / power | O(1) | essential |
parseInt / parseFloat / Number | parseInt(str, radix) | parseFloat(str) | Number(val) | Parse strings to numbers | O(n) | essential |
JSON.stringify / parse | JSON.stringify(val) | JSON.parse(str) | Serialize to / parse from JSON | O(n) | essential |
typeof type guard | typeof value === "string" | Narrows type based on typeof check | O(1) | essential |
instanceof type guard | value instanceof Class | Narrows type based on class check | O(1) | essential |
Array.isArray | Array.isArray(value): boolean | Type guard for arrays | O(1) | essential |
flat / concat | arr.flat(depth?) | arr.concat(...arrays) | [...arr1, ...arr2] | Flatten nested arrays or merge arrays | O(n) | common |
trim | str.trim(): string | Removes whitespace from both ends | O(n) | common |
repeat / padStart / padEnd | str.repeat(n) | str.padStart(len, pad?) | str.padEnd(len, pad?) | Repeat string or pad to length | O(n) | common |
startsWith / endsWith / includes | str.startsWith(s) | str.endsWith(s) | str.includes(s) | Check if string contains/starts/ends with substring | O(n) | common |
replace / replaceAll | str.replace(search, replacement) | str.replaceAll(search, replacement) | Replace first/all occurrences | O(n) | common |
Object.fromEntries | Object.fromEntries(iterable): object | Creates object from key-value pairs | O(n) | common |
String / toString | String(val) | val.toString() | template literal | Convert to string | O(n) | common |
in type guard | "property" in object | Narrows type based on property existence | O(1) | common |
as assertion / as const | value as Type | value as const | Type assertion / const assertion | O(1) | common |
Number.isNaN / isFinite / isInteger | Number.isNaN(v) | Number.isFinite(v) | Number.isInteger(v) | Type-safe number checks | O(1) | common |