JavaScript array methods
The methods are listed in the order they appear in this tutorial page
Lets have a look...
- Syntax by using for loop
// Push() method
[💵,💵,💵].push(💵); // [💵,💵,💵,💵]
// Push() method
[🍏,🍏,🍏].push(🍎); // [🍏,🍏,🍏,🍎]
// unshift() method
[🍏,🍏,🍏].unshift(🍎); // [🍎,🍏,🍏,🍏]
// pop() method
[🍏,🍏,🍏].pop(); // [🍏,🍏]
// shift() method
[🍏,🍏,🍏].shift(); // [🍏,🍏]
// concat() method
[🍏,🍏,🍏].concat(🍎); // [🍏,🍏,🍏,🍎]
// join() method
[🍏,🍏,🍏].join(🍎); // "🍏🍎🍏🍎🍏"
// slice() method
[🍏,🍏,🍏].slice(1); // [🍏,🍏]
// indexOf() method
[🍏,🍏,🍏].indexOf(🍎); // -1
// unshift() method
[🍏,🍏,🍏].unshift(🍎); // [🍎,🍏,🍏,🍏]
// includes() method
[🍏,🍏,🍏].includes(🍎); // false
[🍏,🍎,🍏].includes(🍎); // true
// find() method
[🍏,🍏,🍏].find(element => element === 🍎); // undefined
[3,5,6,8].find(x => x % 2 === 0); // 6
// filter() method
[🍏,🍏,🍏].filter(element => element === 🍎); // []
// findIndex() method
[🍏,🍏,🍏].findIndex(element => element === 🍎); // -1
// map() method
[1,2,3,4].map(element => element * 2); // [2,4,6,8]
// every() method
[🍏,🍏,🍏].every(element => element === 🍏); // true
// some() method
[🍏,🍏,🍏].some(element => element === 🍎); // false
// reverse() method
[🍏,🍏,🍏].reverse(); // [🍏🍏🍏]
values() method
// values() method
// This method return an iterator that provides the values for each index in the array.
// It takes no arguments.
const arr = ['apple','banana','cherry'];
const iterator = arr.values();
for(const value of iterator)
{
console.log(value);
}
// Output :
apple
banana
cherry
length() method
// length() method
// This method return the length of the array.
const arr = ['apple','banana','cherry'];
console.log(arr.length);
// Output : 3
reverse() method
// reverse() method
// This method reverse the order of the elements in the array.
const arr = ['apple','banana','cherry'];
arr.reverse();
console.log(arr);
// Output : ['cherry', 'banana', 'apple']
sort() method
// sort() method
// This method sort the elements of the array and return the sorted array.
const arr = ['cherry','apple', 'banana'];
arr.sort();
console.log(arr);
// OutPut : ['apple', 'banana', 'cherry']
at() method
// at() method
// This method returns the element of an specified index from the array.
// It takes one argument : Index
const arr = ['cherry','apple', 'banana'];
var element = arr.at(2);
console.log(element);
// OutPut : banana
fill() method
// fill() method
// This method fill all the elements of an array from start index to end index with a static value.
// It takes up to 3 argument : ( value, startIndex, endIndex - 1 )
// value : The value to fill with.
// please find the below example...
const arr = ['cherry','apple', 'banana'];
arr.fill('orange',1,3);
console.log(arr);
// OutPut : ['cherry', 'orange', 'orange']
0 Comments