10 JavaScript String And Array Methods Every Developer Should Know

Saiful Islam Sojib
3 min readMay 5, 2021
JavaScript String and Array

Hi! Everyone, Today I will explore 10 JavaScript string and array methods that every JavaScript developer should know. So, let's go to the main topic.

1. String.charAt()

String.charAt() returns a new character string from the main string that’s index provided by the parameters. The index should be an integer number between 0 and str. length - 1.

If no index is provided return the first character because the initial index is 0. If the index is out of range, return an empty string.

Code Examples:

const str = "My name is Khan.";console.log(str.charAt(3)) // output, "n"
console.log(str.charAt(str.length - 1)) // output, "."

2. String.split()

String.Split() returns an array by divides a String. It has two parameters separator and limit. It divides a String by the separator.

If no parameter is provided, return an array with one string element. If the separator is an empty string, returns an array with all individual character elements. If the separator is space “ ”, returns all individual word elements. It because separate by space. You can provide any separator to separate a string.

Code Examples:

const str = "My name is Khan.";console.log(str.split()) // output, [ "My name is Khan."]console.log(str.split("")) // output, [ "M", "y", " ", "n", "a", "m", "e", " ", "i", "s", " ", "K", "h", "a", "n", "."]console.log(str.split(" "))
// output, [ "My", "name", "is", "Khan."]
console.log(str.split("is")) // output, [ "My name", "Khan."]

3. String.toUpperCase()

String.toUpperCase() returns an uppercase string from the main string.

Code Example:

const str = "My name is Khan.";console.log(str.toUpperCase()) // output, "MY NAME IS KHAN."

4. String.trim()

String. trim() returns a string from the main string which removes whitespaces from the start and ends of both sides.

Code Example:

const str = "   My name is Khan.  ";console.log(str.toUpperCase()) // output, "My name is Khan."

5. Array.filter()

Array. filter() returns a new array from the main array which is filtered by one or more conditions. This method should provide a function that should return a condition.

code Examples:

const arr = [1, 2, 5, 8, 10];console.log(arr.filter((num)=> num>5)) //output, [8, 10]

6. Arrar.find()

Array. find() returns an element from the array which is found by one or more conditions. This method should provide a function that should return a condition. It always returns the first matching element.

code Examples:

const arr = [1, 2, 5, 8, 10];console.log(arr.filter((num)=> num > 5)) //output, 8

7. Array.join()

Array. join() returns a string from the array. It has a parameter, separator.

If no separator is provided, return a string separated by a comma with all elements. If the separator is an empty string, return a string without separation with all elements. You can provide any separator to separate the output string.

code Examples:

const arr = [1, 2, 5, 8, 10];console.log(arr.join()) //output, "1,2,5,8,10"console.log(arr.join("")) //output, "125810"console.log(arr.join(" ")) //output, "1 2 5 8 10"console.log(arr.join(" + ")) //output, "1 + 2 + 5 + 8 + 10"

8.Array.forEach()

Array.forEach() use for looping an array but it doesn’t return anything. It should provide a function and this function’s parameters are an element, index, and the main array.

code Examples:

const arr = [1, 2, 5, 8, 10];arr.forEach((num, index)=> console.log(num, index))

9.Array.map()

Array. map() use for looping an array and return an array. It should provide a function and this function’s parameters are an element, index, and the main array. One difference between forEach and map is map returns an array but forEach doesn’t return anything.

code Examples:

const arr = [1, 2, 5, 8, 10];console.log(arr.map((num, index)=> return(num * index)))// output, [0, 2, 10, 24, 40]

10. Array.push()

Array.push() Insert an element at the end of the array and return the new length of the array.

code Examples:

const arr = [1, 2, 5, 8, 10];console.log(arr.push(12)) //output, 6console.log(arr) // output, [1, 2, 5, 8, 10, 12]

--

--