JavaScript Cheatsheet
48 essential methods for coding interviews
Showing 48 of 48 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
push | array.push(element1, ...elementN) | Adds elements to end of array, returns new length | O(1) amortized | essential |
pop | array.pop() | Removes and returns last element | O(1) | essential |
shift | array.shift() | Removes and returns first element | O(n) | essential |
slice | array.slice(start?, end?) | Returns shallow copy of portion of array (end not included) | O(n) | essential |
splice | array.splice(start, deleteCount?, ...items) | Removes/replaces elements in place, returns removed elements | O(n) | essential |
indexOf | array.indexOf(element, fromIndex?) | Returns first index of element, or -1 if not found | O(n) | essential |
includes | array.includes(element, fromIndex?) | Returns true if array contains element | O(n) | essential |
reverse | array.reverse() | Reverses array in place and returns it | O(n) | essential |
join | array.join(separator?) | Joins all elements into a string | O(n) | essential |
fill | array.fill(value, start?, end?) | Fills array with static value | O(n) | essential |
Array.from | Array.from(iterable, mapFn?) | Creates array from iterable or array-like object | O(n) | essential |
map | array.map((element, index, array) => newValue) | Creates new array with results of calling function on every element | O(n) | essential |
filter | array.filter((element, index, array) => boolean) | Creates new array with elements that pass the test | O(n) | essential |
reduce | array.reduce((acc, curr, index, array) => newAcc, initialValue) | Reduces array to single value by applying function | O(n) | essential |
find | array.find((element, index, array) => boolean) | Returns first element that satisfies the test, or undefined | O(n) | essential |
findIndex | array.findIndex((element, index, array) => boolean) | Returns index of first element that satisfies test, or -1 | O(n) | essential |
sort | array.sort((a, b) => number) | Sorts array in place and returns it | O(n log n) | essential |
Binary Search Pattern | while (left <= right) { mid = Math.floor((left + right) / 2); ... } | Efficient search in sorted array | O(log n) | essential |
split | string.split(separator, limit?) | Splits string into array by separator | O(n) | essential |
substring | string.substring(start, end?) | Returns part of string between start and end (end exclusive) | O(n) | essential |
slice (string) | string.slice(start, end?) | Extracts section of string (end exclusive) | O(n) | essential |
charAt / charCodeAt | string.charAt(index) / string.charCodeAt(index) | Returns character or Unicode value at index | O(1) | essential |
String.fromCharCode | String.fromCharCode(num1, ...numN) | Creates string from Unicode values | O(n) | essential |
includes (string) | string.includes(searchString, position?) | Returns true if string contains substring | O(n*m) | essential |
indexOf (string) | string.indexOf(searchValue, fromIndex?) | Returns first index of substring, or -1 | O(n*m) | essential |
toLowerCase / toUpperCase | string.toLowerCase() / string.toUpperCase() | Converts string to lower/upper case | O(n) | essential |
Object.keys | Object.keys(obj) | Returns array of object's own enumerable property names | O(n) | essential |
Object.values | Object.values(obj) | Returns array of object's own enumerable property values | O(n) | essential |
Object.entries | Object.entries(obj) | Returns array of [key, value] pairs | O(n) | essential |
hasOwnProperty / in | obj.hasOwnProperty(key) / key in obj | Checks if property exists on object | O(1) | essential |
Set | new Set(iterable?) | Collection of unique values | O(n) to construct | essential |
Set.add / delete / has | set.add(value) / set.delete(value) / set.has(value) | Add, remove, or check for value in Set | O(1) average | essential |
Map | new Map(iterable?) | Collection of key-value pairs with any key type | O(n) to construct | essential |
Map.set / get / has / delete | map.set(key, value) / map.get(key) / map.has(key) / map.delete(key) | Core Map operations | O(1) average | essential |
Math.max / Math.min | Math.max(...values) / Math.min(...values) | Returns largest/smallest of given numbers | O(n) | essential |
Math.floor / ceil / round / trunc | Math.floor(x) / Math.ceil(x) / Math.round(x) / Math.trunc(x) | Rounds number down/up/nearest/toward zero | O(1) | essential |
Math.abs | Math.abs(x) | Returns absolute value | O(1) | essential |
parseInt / parseFloat | parseInt(string, radix?) / parseFloat(string) | Parses string to integer or float | O(n) | essential |
Number / String / Boolean | Number(value) / String(value) / Boolean(value) | Converts value to number, string, or boolean | O(n) | essential |
unshift | array.unshift(element1, ...elementN) | Adds elements to beginning of array, returns new length | O(n) | common |
concat | array.concat(array2, ...arrayN) | Merges arrays and returns new array | O(n) | common |
some | array.some((element, index, array) => boolean) | Returns true if at least one element passes the test | O(n) | common |
every | array.every((element, index, array) => boolean) | Returns true if all elements pass the test | O(n) | common |
forEach | array.forEach((element, index, array) => void) | Executes function once for each element | O(n) | common |
replace / replaceAll | string.replace(pattern, replacement) | Replaces first (or all) occurrences of pattern | O(n) | common |
trim / trimStart / trimEnd | string.trim() | Removes whitespace from both ends | O(n) | common |
Object.assign | Object.assign(target, ...sources) | Copies properties from sources to target | O(n) | common |
JSON.stringify / parse | JSON.stringify(obj) / JSON.parse(string) | Converts object to JSON string and back | O(n) | common |