Logo

JavaScript Cheat Sheet

Print

Arrays

Array Create

const ar = ['a','b','c']

Array Access (Properties)

ar[1] // 'b'
lengthar.length // 3 Documentation
indexOfar.indexOf('b') // 1 Documentation
lastIndexOfar.lastIndexOf('b') // 1 Documentation
includesar.includes('b') // true Documentation
keysar.keys.next().value // 0 Documentation
valuesar.values.next().value // 'a' Documentation
entriesar.entries.next().value // [0,'a'] Documentation
isArrayArray.isArray(ar) // true Documentation

Array <-> String

split'a,b,c'.split(',') // ['a','b','c'] Documentation
joinar.join(',') // 'a,b,c' Documentation

Array Modify

pushar.push('d') // ['a','b','c','d'] Documentation
unshiftar.unshift('ø') // ['ø','a','b','c'] Documentation
popar.pop() // ['a','b'] Documentation
shiftar.shift() // ['b','c'] Documentation
slicear.slice(1,1) // ['a','c'] ar not modified Documentation
splicear.splice(1,1) // ['a','c'] Documentation
fillar.fill('e',1) // ['a','e','e'] Documentation
copyWithinar.copyWithin(0,1) // ['b','c','c'] Documentation
concatar.concat(['d','e']) // ['a','b','c','d','e'] Documentation
sort['z','y','x'].sort() // ['x','y','z'] Documentation
reversear.reverse() // ['c','b','a'] Documentation

Array Loop

forEachar.forEach(el => console.log(el)) Documentation

Array Construction

filterar.filter(el => el === value) Documentation
map (where)ar.map(el => el === value) Documentation
map (modify)ar.map(el => el * 2) Documentation
findar.find(el => el * 2) Documentation
findIndexar.findIndex(el => el * 2) Documentation
every[1,2,3,4].every(el => el < 3) // [1,2] Documentation
somear.some(el => el === value) Documentation
reducear.reduce((a, b) => a + b) Documentation
reduceRightar.reduceRight((a, b) => a + b) Documentation
flatar.flat() Documentation
flatMapar.flatMap(el => [el * 2]) Documentation
Spread syntax (...)[...[1,2,3], 4] // [1,2,3,4] Documentation

Dates & Times

String Access

Date.nowDate.now() // 1655663289559 Documentation
Date.parseDate.parse('1969-07-20T20:17:00.100Z') Documentation
new Datenew Date('July 20, 1975 20:17:00') Documentation
getTimemoonLand.getTime() // 175115820000 Documentation
getHoursmoonLand.getHours() // 20 Documentation
getMinutesmoonLand.getMinutes() // 17 Documentation
getDaymoonLand.getDay() // 0 Sunday Documentation
getDatemoonLand.getDate() // 20 Documentation
getMonthmoonLand.getMonth() // 6 Documentation
getUTCFullYearmoonLand.getUTCFullYear() // 1969 Documentation

JSON

JSON.parseJSON.parse('{'x':5}') // {test:5} Documentation
JSON.stringifyJSON.stringify('{x:5}') // {'x':5} Documentation

Strings

String Access

const st = 'Dig'
lengthst.length() // 3 Documentation
charAtst.charAt(1) // 'i' Documentation
indexOfst.indexOf('i') // 1 Documentation
lastIndexOfst.lastIndexOf('i') // 1 Documentation
includesst.includes('ig') // true Documentation
startsWithst.startsWith('Di') // true Documentation
endsWithst.endsWith('ig') // true Documentation
searchst.search(/[a-z]/) // 1 Documentation
match'SuPEr'.match(/[A-Z]/g) // ['S','P','E'] Documentation

String Modify

slicest.slice(1,3) // 'ig' Documentation
substringst.substring(1,3) // 'ig' Documentation
replacest.replace('i',o') // 'Dog' Documentation
replaceAll'door'.replaceAll('o','e') // 'deer' Documentation
trim' hello '.trim() // 'hello' Documentation
trimStart' hello '.trimStart() // 'hello ' Documentation
trimEnd' hello '.trimEnd() // ' hello' Documentation
padStartst.padStart(6,'#') // '###Dig' Documentation
padEndst.padEnd(6,'#') // 'Dig###' Documentation
toLowerCasest.toLowerCase() // 'dig' Documentation
toUpperCasest.toUpperCase() // 'DIG' Documentation

Numbers & Math

Numbers

NumberNumber('3.7') // 3.7 Documentation
StringString(3.7) // '3.7' Documentation
parseFloatparseFloat('3.7') // 3.7 Documentation
parseIntparseInt('3.7') // 3 Documentation
BooleanBoolean(0) // false Documentation
toFixed12.3456.toFixed(2) // 12.35 Documentation
toPrecision12.3456.toPrecision(5) // 12.346 Documentation

Math

Math.roundMath.round(3.7) // 4 Documentation
Math.floorMath.floor(3.7) // 3 Documentation
Math.ceilMath.ceil(3.2) // 4 Documentation
Math.truncMath.trunc(42.84) // 42 Documentation
Math.minMath.min(1,2,3) // 1 Documentation
Math.maxMath.max(1,2,3) // 3 Documentation
Math.randomMath.random() // 0.33529 Documentation
Math.powMath.pow(3,2) // 9 Documentation
Math.sqrtMath.sqrt(16) // 4 Documentation
Math.absMath.abs(-5) // 5 Documentation

Fetch, Promise & Async/Await

Fetch (Web API)

fetchconst p1 = fetch('http://…')
  .then(resp => resp.json())
  .then(data => data)
  .catch(err => console.log(err)) Documentation

Promise

Promisenew Promise((resolve, reject) => { … }) Documentation
Promise.allPromise.all([p1,p2]).then((data) => { … }) Documentation

Asyn/Await

async/awaitconst getSomething = async () => {
  const resp = await fetch(req)
  const data = await resp.json()
  return data
}
getSomething().then(data => {
  const result = data.find(o => o.n==='a')
  console.log(result)
}) Documentation