String & Array methods I recently learned - PART 2
Tags: codenewbie, javascript, arrays
Published: 2021-08-19 06:24:14

Hi fellow code newbies! Yes! This is a 2nd part of my recent learnings of JavaScript methods! Mostly for this blog are Array methods! I didn't realize how much more methods I could've used in algorithms until I saw these!

### .lastIndexOf()
Returns the index/position of the element that meets the condition. In the example below, it returns the beginning index where the string "name" last occured.
![code application in array & string](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvkzdlvkhyw9y518q9ze.png)
***

### .match()
Returns an array with key-value pairs if function call has a string as an argument and query.
![match code application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3xbqdv9slc64jgpl2sp.png)

You can also use regular expression to filter out what you need to be matched from a string, and it will return an array of matched elements.
![Trying out with regex](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83dwk8hmuqym4rfcwqle.png)
***

### .flat()
If you've heard of flattening the array, this is the method for it. Simple way to explain it, is that it concise a nested array into one. Sets the argument by default of 1.

![method in application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fki468a3smzah3rhfi42.png)
<figcaption>Takes in an integer argument as the depth on how much into the array it will flatten.</figcaption>
***

### .some()
A boolean and returns true if any of the element from the array makes the function passed in true. This doesn't manipulate the array and may best work for validations.
![Some method in application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opittkzorjz1icc5jof3.png)
***

### .every()
A boolean and will return true if **ALL** of the elements in the array meets the function passed in as the argument. If the array is empty, it will still return true no matter the condition.
![method in application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ep7r97udeau8v7iyjfhd.png)
***

### .copyWithin()
This is an interesting one. It takes in 1-3 arguments depending on what you need. 
`copyWithin(target)`
`copyWithin(target, start)`
`copyWithin(target, start, end)`
It copies part of the array and returns an array with all the copied parts you need & keeps its length same with the original array.
![CopyWithin in 3 examples](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0g6l4hsmgl6m3qphkmsh.png)
<figcaption> Default value of end if left out is array.length </figcaption>

***


**IDE themes used for code examples:**
- [Dracula](https://marketplace.visualstudio.com/items?itemName=dracula-theme.theme-dracula)
- [Winter is Coming](https://marketplace.visualstudio.com/items?itemName=johnpapa.winteriscoming) - Dark Blue
- [Sorcerer](https://sorcerer.mage.tools/)
- [Kabukichō](https://marketplace.visualstudio.com/items?itemName=VictoriaDrake.kabukicho)
- [Firefly Pro](https://marketplace.visualstudio.com/items?itemName=ankitcode.firefly)
- [Hopscotch Mono](https://marketplace.visualstudio.com/items?itemName=idleberg.hopscotch)
- [MDN - Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) - for further examples if you need