Methods Flashcards
.forEach()
- Used to iterate over each element in an array and perform a specified action
- It executes a provided callback function once for each element in the array, in ascending order
- Does not return a new array
var numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number) {
console.log(number);
});
Output:
1
2
3
4
5
.map()
- Used to iterate over each element in an array, apply a transformation or operation to each element
- Return a new array containing the results of that transformation.
- It executes a provided callback function once for each element in the array and creates a new array based on the return values of the callback function.
var numbers = [1, 2, 3, 4, 5];
var multipliedNumbers = numbers.map(function(number) {
return number * 2;
});
console.log(multipliedNumbers);
Output:
[2, 4, 6, 8, 10]
.push()
- Adds one or more elements to the end of an array and returns the new length of the array.
.pop()
Removes the last element from an array and returns that element.
.shift()
Removes the first element from an array and returns that element. It also shifts all subsequent elements to a lower index.
.unshift()
Adds one or more elements to the beginning of an array and returns the new length of the array. It also unshifts existing elements to a higher index.
.slice()
Returns a shallow copy of a portion of an array into a new array. It takes two arguments: the starting index (inclusive) and the ending index (exclusive).
.join()
Joins all elements of an array into a string using a specified separator and returns the resulting string.
.concat()
Combines (or concatinates) two or more arrays and returns a new array.
.filter()
Creates a new array with all elements that pass a provided test.
.split()
Splits a string into an array of substrings based on a specified separator.
.trim()
Removes whitespace from both ends of a string.
.includes()
Checks if a string contains a specified substring and returns true or false.
.charAt()
Returns the character at a specified index in a string.