Array Cheatsheet in JavaScript🚀
Tags: javascript, webdev, beginners, programming
Published: 2022-03-28 18:56:56

In any programming language if we have a collection of elements or items, then we will define them as array. they can store data such as string, numbers, object as an collection of element and we can retrieve them whenever and however we want.
I will discuss about what can we do using array in JavaScript. so first of all let's create a array in JavaScript.
There are many ways to create array in JavaScript i.e. -

**1. Basic Way**

```
`const animals = ['🙈','🙉','🙊','🐵']`
```



**2. Array Constructor**

```
`const food = new Array('🍇','🍈','🍉','🍊','🍌')`
```


**3. Empty Array**

```
`const emptyArray = []`
```


Now, we know how to create an array in JavaScript and now let's dive into some methods of Array.

**1. indexOf()** - we can find an item's index

```
`const animals = ['🐶','🐱','🦁','🐷']
animals.indexOf('🦁')     //2 `
```

**2. join()** - create a string from array items and also can add words between them like 'and','or'.

```
`const animals = ['🐶','🐱','🦁','🐷']
animals.join('loves')   // '🐶 loves 🐱 loves 🦁 loves 🐷' `
```

**3. slice()** - split an array at given index(es)

```
`const animals = ['🐶','🐱','🦁','🐷']
animals.slice(1);    //['🐱','🦁','🐷']
animals.slice(1,2);   //[ '🐱' ]
console.log(animals)  //[ '🐶', '🐱', '🦁', '🐷' ] `
```

**4. splice()** - same as slice and you can also insert new items

```
`const animals = ['🐶','🐱','🦁','🐷']
animals.splice(1,2, '🍊') )   //[ '🐱', '🦁' ]
console.log(animals)  //[ '🐶', '🍊', '🐷' ]`
```

_as you can see that slice and splice looks similar but slice don't overwrite or change original array but splice does. _

**5. forEach()** - loop over an array and access each item

```
`const animals = ['🐶','🐱','🦁','🐷']
animals.forEach(pet => console.log(pet))`
```

**6. Map()** - loop over an array and access each item and return value without mutating original array

```
`const animals = ['🐶','🐱','🦁','🐷']
animals.map(pet => console.log(`${pet} is cute`))`
```

_As you can see in first glance both forEach and map method are same, but as you can see in map method we are mutating array instead we are creating a new array and using map we can return something._

**7. flat()**- It flatten an array to a single dimension.

```
`const animals = ['🐶','🐱','🦁', ['🙊','🙉'],'🐷']`
```
as you can see we have array under an array , so how do we get a single dimension array.

```
`console.log(animals.flat()) //['🐶','🐱','🦁','🙊','🙉','🐷']`
```

**8. filter()** - create a new array based on a filter.

```
`const animals = ['🐶','🐱','🦁','🐶']
const dog = animals.filter(dog => dog === '🐶') 
console.log(dog)`
```

**9. reduce()** - run a callback on each item and reduce the array to a single value.

```
`const numbers = [1,6,7,1,3,4]
const total = numbers.reduce((total,currentval) => total + currentval);   //22 `
```

_Ok, so what's happening here is we are taking every number one by one which is our currentval and adding it into our total._

**10. findIndex()** - finds the index of an item based on a condition

```
`const animals = ['🐶','🐱','🦁','🙉']
const lionIndex = animals.findIndex(lion => lion === '🦁')
console.log(lionIndex)   //2`
```


## Conclusion

So they are the most used array methods which I use in my code and I hope you will find them useful and we are all learning, I missed a lot of methods here but doing research is a part of the process 🧑‍💻🧑‍💻