JavaScript CheatSheet Flashcards
cheat sheet
What are the (7) Array finding functions?
.indexOf()
.lastIndexof()
.length()
.at()
.find()
.findIndex()
.includes()
What are the (4) Creating array functions
.slice()
.of()
.flat()
.flatMap()
What are the (6) Adding array functions?
.splice()
.push()
.copyWithin()
.fill()
.concat()
.unshift()
Name (3) Removing array functions
.shift()
.pop()
.splice()
Name (2) Re-arranging array functions
.reverse()
.sort()
What are (4) functions that can be used for Misc. tasks?
.join()
.toString()
.isArray()
.indexOf()
What are (8) looping array functions
.filter()
.map()
.reduce()
.forEach()
.reduceRight()
.some()
.every()
.entries()
Adding Methods
.push()
.unshift()
.splice()
Reorganizing methods
.copyWithin()
.slice()
.splice()
Combining methods
.concat()
…
Removing methods
.pop()
.shift()
.splice()
Filter/Searching methods
.filter()
.find()
.findIndex()
.indexOf()
.lastIndexOf()
Sort alphabetically.
let a = ['d', 'j', 'a', 'b', 'c', 'g'] a.sort() console.log(a) // ["a", "b", "c", "d", "g", "j"]
Sort in reversed alphabetical order.
let a = ['d', 'j', 'a', 'b', 'c', 'g'] a.sort().reverse() console.log(a) // [ "j", "g", "d", "c", "b", "a"]
Sort numerically.
let a = [5, 10, 7, 1, 3, 2] a.sort((a, b) => a - b) console.log(a) // [ 1, 2, 3, 5, 7, 10]
Descending numerical sort (flip the a
and b
around).
let a = [5, 10, 7, 1, 3, 2] a.sort((a, b) => b - a) console.log(a) // [10, 7, 5 ,3, 2, 1]
Add anything to the end of an array.
.push()
let a = [] let b = {} a.push(b) console.log(a[0] === b) // true
Add more than one item at a time to the end of an array
.push()
let x = ['a'] x.push('b', 'c') console.log(x) // ['a', 'b', 'c']
Add to the beginning of array and returns the new length of the array.
.unshift()
let x = ['c', 'd'] x.unshift('a', 'b') // 4 console.log(x) // ['a', 'b', 'c', 'd']
Add element to an arbitrary location (second param 0).
.splice()
let a = [1, 2, 3, 4] a.splice(2, 0, 'foo') console.log(a) // [1, 2, "foo", 3, 4]
Copy part of an array to another location within the same array.
.copyWithin()
let a = ['a', 'b', 'c', 'd', 'e'] // target, start, end array1.copyWithin(0, 3, 4) console.log(a) // ["d", "b", "c", "d", "e"]
Returns a portion of an array selected with the start
and end
parameters.
.slice()
let a = ['ant', 'bison', 'camel', 'duck', 'elephant'] console.log(animals.slice(2)) // ["camel", "duck", "elephant"] console.log(animals.slice(2, 4)) // ["camel", "duck"]
Replace an arbitrary element in array (second param 1).
.splice()
let a = [1, 2, 3, 4] a.splice(2, 1, 'foo') console.log(a) // [1, 2, "foo", 4]
Create a new array from two arrays.
.concat()
let x = ['a', 'b', 'c'] let y = ['d', 'e', 'f'] let z = x.concat(y) console.log(z) // ['a', 'b', 'c', 'd', 'e', 'f']