Skip to content

godspeedelbow/js-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 

Repository files navigation

Javascript Cheat Sheet

String

access

charAt(index) - returns the character at index

substring(start, end) - returns string from start to end - 1. if start > end, they are swapped

slice(start, end) - like substring, but without the swapping. instead if start >= end, an empty string is returned

substr(start, count) - returns string from start to start + count

search

endsWith(str) - returns whether end of string matches str

startsWith(str) - returns whether start of string matches str

includes(str) - returns whether string matches str anywhere

indexOf(str) - returns first index at where str was found

lastIndexOf(str) - returns last index at where str was found

match(regex) - returns regex matches for string (regex is converted to RegEx, if isn't one yet). Wrapper for RegExp.exec.

search(regex) - returns index of first regex match (regex is converted to RegEx, if isn't one yet)

map

repeat(count) - returns string repeated count times

split(str) - returns array of chunks, split on str

replace(regex/str, str2) - replaces regex/str matches with str2

Array

mutation

push(elem) - adds elem to end of array, returns new array length

unshift(elem) - adds elem to beginnging of array, returns new array length

pop() - removes and returns last element in the array

shift() - removes and returns first element in array

splice(start, count, ...elems) - remove count number of elements starting at start, if elems provided, insert them at start

reverse() - reverses the array

fill(elem, start, end) - replace elems with elem from start to end

map

map(iteratee(el, idx, arr)) - returns a new array with iteratee applied to each element

concat(...elems) - merges array with elems into new array, arrays in elems first unpacked to values

slice(start, end) - returns array from index start to end - 1. if start >= end, an empty array is returned

filter(iteratee(el, idx, arr)) - returns an array composed of all elements for which iteratee returns true

reduce/search

reduce(iteratee(acc, el, idx, arr), startAcc) - returns a value composed of startAcc and running iteratee on each element in array

reduceRight - like reduce but then iterated from end of array to beginning

find(iteratee(el, idx, arr)) - returns the value of the first element in the array that satisfies iteratee. Otherwise undefined

indexOf(elem) - returns first index at where elem was found (using ===)

lastIndexOf(elem) - returns last index at where elem was found (using ===)

every(iteratee(el, idx, arr)) - returns whether iteratee returns true for every element in array

some(iteratee(el, idx, arr)) - returns whether iteratee returns true for any element in array

join(str) - returns string with each elem in array joined by str

includes(elem, start) - find elem in array, if start is undefined or < 0, the entire array is searched

RegExp

Creation

  • new RegExp(str, modifiers)

  • /str/modifiers

str is a regex which consists of one or more Tokens. modifiers are optional

Methods

test(str) - returns whether the regex has any matches in str

exec(str) - search for any matches in str, returns null if no match found. Updates regex object

Tokens

abc… - Letters

123… - Digits

\d - Any Digit

\D - Any Non-digit character

. - Any Character

\. - Period

[abc] - Only a, b, or c

[^abc] - Not a, b, nor c

[a-z] - Characters a to z

[0-9] - Numbers 0 to 9

\w - Any Alphanumeric character

\W - Any Non-alphanumeric character

{m} - m Repetitions

{m,n} - m to n Repetitions

* - Zero or more repetitions

+ - One or more repetitions

? - Zero or one repetitions

\s - Any Whitespace

\S - Any Non-whitespace character

^…$ - Starts and ends

(…) - Capture Group

(a(bc)) - Capture Group and Sub-group

(abc|def) - Matches abc or def

Modifiers

i - performs case-insensitive matching

g - performs a global match (find all matches rather than stopping after the first match)

m - performs multiline matching

Reference

async/await

async function foo() {
    return 42;
}

is syntactic sugar for:

function foo() {
    return new Promise(resolve => {
        resolve(42);
    });
}

and this:

const data = await something();
// ...do something...

is syntactic sugar for:

something().then(data => {
    // ...do something...
});
  • async keyword makes a function return a promise which is resolved when the function returns
  • await keyword waits (locally blocks) for an async function (or promise) to resolve and then replaces the promise with the value it resolved to and continues code execution

More info, see: Tips for using async functions

About

stuff to remember, stuff to look up

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published