Ads by ProfitSence
Close

JavaScript Array methods CheatSheet

Last Updated on Wednesday 5th Oct 2022

Creating a new Array from an existing Array

slice method

			
					['A','B','C','D'].slice(1, 3)
// [ 'B', 'C' ]

			
	

filter method

			
					['A','B','C','D','B'].filter(x => x==='B')
// [ 'B', 'B' ]

			
	

map method

			
					['A','B','C','D'].map(x => x)
// [ 'A', 'B', 'C', 'D' ]

			
	

flatmap method

			
					['A','B','C','D'].flatMap(x => [x,x])
// [
//  'A', 'A', 'B',
//  'B', 'C', 'C',
//  'D', 'D'
// ]

			
	

Finding Array elements

includes method

			
					['A','B','C','D'].includes('B')
// true

			
	

indexOf method

			
					['A','B','C','D'].indexOf('B')
// 1

			
	

lastIndexOf method

			
					['A','B','C','D'].lastIndexOf('C')
// 2

			
	

find method

			
					['A','B','C','D'].find(x=> x === 'C')
// C

			
	

findIndex method

			
					['A','B','C','D'].findIndex(x=> x === 'D')
// 3