Skip to content

Instantly share code, notes, and snippets.

@spyesx
Created March 21, 2022 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spyesx/c518acf050fd8d3d2a562d027e7dff42 to your computer and use it in GitHub Desktop.
Save spyesx/c518acf050fd8d3d2a562d027e7dff42 to your computer and use it in GitHub Desktop.
Common array functions in javascript
[1, 2, 3].push(4)
// [1,2,3,4]
[1, 2, 3].pop()
// [1,2]
[1, 2, 3].shift()
// [2,3]
[1, 2, 3].unshift(0)
// [0,1,2,3]
['a', 'b'].concat('c')
// ['a','b','c']
['a', 'b', 'c'].join('-')
// 'a-b-c'
['a', 'b', 'c'].slice(1)
// ['b','c']
['a', 'b', 'c'].indexOf('b')
// 1
['a', 'b', 'c'].includes('c')
// true
[3, 5, 6, 8].find((n) => n % 2 === 0)
// 6
[2, 4, 3, 5].findIndex((n) => n % 2 !== 0)
// 2
[3, 4, 8, 6].map((n) => n * 2)
// [6,8,16,12]
[1, 4, 7, 8].filter((n) => n % 2 === 0)
// [4,8]
[2, 4, 3, 7].reduce((acc, curr) => acc + curr)
// 16
[2, 3, 4, 5].every((x) => x < 6)
// true
[3, 5, 6, 8].some((n) => n > 6)
// true
[1, 2, 3, 4].reverse()
// [4,3,2,1]
[3, 5, 7, 8].at(-2)
// 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment