Some useful array and string method you need to know…

Aman
3 min readNov 2, 2020
Some array method
Photo from internet on Unsplash

What is an array?

Array is a kind of data structure that can store sequential collection of multiple elements. In javascript arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.

Here’s are some important array methods which we commonly use in javascript program.

filter()

The array.filter() method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method.


const numbers = [1, 2, 3, 4, 8, 9, 12, 18, 21];
const evenNumbers = numbers.filter(num => num%2 == 0);
console.log(evenNumbers);
Output:
[ 2, 4, 8, 12, 18 ]

find()

The array.find() method is used to get the value of the first element in the array which satisfies the provided condition.


const numbers = [1, 2, 3, 4, 8, 9, 12, 18, 21];
const evenNumbers = numbers.filter(num => num%2 == 0);
console.log(evenNumbers);
Output:
[ 2 ]

map()

The array.map() method creates a new array with the results of called function for every array element.


const numbers = [1, 2, 3, 4, 9, 16];
// pass a function to map
const squareNumbers = numbers.map(num => num * num);
console.log(squareNumbers);
output:
Array [2, 8, 18, 32]

slice()

The array.slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged.

const cars = ["BMW", "Ferrari", "Mercedes", "Rolls-Royce", "Audi", "Toyota", "Ford", "Volkswagen"]
console.log(cars.slice(4));
console.log(cars.slice(1,5));
console.log(cars);
Output:
[ 'Audi', 'Toyota', 'Ford', 'Volkswagen' ]
[ 'Ferrari', 'Mercedes', 'Rolls-Royce', 'Audi' ]
[
'BMW',
'Ferrari',
'Mercedes',
'Rolls-Royce',
'Audi',
'Toyota',
'Ford',
'Volkswagen'
]

splice()

The array.splice() method is used to modify the contents of an array by removing the existing elements and/or by adding new elements.

const cars = ["BMW", "Ferrari", "Mercedes", "Rolls-Royce","Audi", "Toyota", "Ford","Volkswagen"]
cars.splice(2, 0, "Mercedes")
console.log(cars);
cars.splice(3,5)
console.log(cars);
Output:[
'BMW', 'Ferrari',
'Mercedes', 'Mercedes',
'Rolls-Royce', 'Audi',
'Toyota', 'Ford',
'Volkswagen'
]
[ 'BMW', 'Ferrari', 'Mercedes', 'Volkswagen' ]

forEach()

forEach() calls a provided callback function once for each element in an array in ascending order.


const numbers = [1, 2, 3, 4, 5,6, 7, 8, 9, 10];
numbers.forEach(function(number) {
console.log(number);});
Output:
1
2
3
4
5
6
7
8
9
10

push() & unshift()

These two methods are use to add elements. In push() method added element at the end of the array and Unshift add an element at the beginning of an array.


const language=["C++","C", "Python", "Ruby"];
console.log(language);
//use array push method insert an element at end
lang.push("Java");
console.log("After using push method array is: ",language);
//use array unshift method insert an element at start
lang.unshift("JavaScript");
console.log("After using push method array is: ",language);
Output:
[ 'C++', 'C', 'Python', 'Ruby' ]
After using push method array is: [ 'C++', 'C', 'Python', 'Ruby', 'Java' ]
After using push method array is: [ 'JavaScript', 'C++', 'C', 'Python', 'Ruby', 'Java' ]

pop() & shift()

Pop method removes the last element of an array and shift method removes first element of an array.

const language =["JavaScript" ,"C++","C" ,"Python", "Ruby", "Java"];
console.log(language);
//use array pop method remove an element at end
language.pop();
console.log("After using pop method array is: ",language);
//use array shift method remove an element at start
language.shift();
console.log("After using pop method array is: ",language);
Output:
[ 'JavaScript', 'C++', 'C', 'Python', 'Ruby', 'Java' ]
After using pop method array is: [ 'JavaScript', 'C++', 'C', 'Python', 'Ruby' ]
After using pop method array is: [ 'C++', 'C', 'Python', 'Ruby' ]

What is String?

The String object is used to represent and manipulate a sequence of characters

--

--