DEV Community

Cover image for JavaScript - basic array operations with examples
Dirask-JavaScript
Dirask-JavaScript

Posted on

JavaScript - basic array operations with examples

Have you ever had any difficulties using basic array methods in JavaScript?

In today's post I will try to explain the basic operations on arrays as simply as possible, stay tuned! πŸ”₯

Before we start, I would highly recommend you to check out the runnable examples for the solution on our website:
JavaScript - basic array operations

Below I've presented the six most common methods used on arrays that you may find useful:

  • map()
  • filter()
  • find()
  • fill()
  • some()
  • every()

1. map() method

In this example, I've used the map() method to create a new array filled with the results of calling a provided function on every element in the calling array.

const array1 = ['🟦', '🟦', '🟦', '🟦'];

const array2 = array1.map((item) => 'πŸ”΅');

console.log('array1: ' + array1);
console.log('array2: ' + array2);
Enter fullscreen mode Exit fullscreen mode

Output:

array1: 🟦,🟦,🟦,🟦
array2: πŸ”΅,πŸ”΅,πŸ”΅,πŸ”΅
Enter fullscreen mode Exit fullscreen mode

2. filter() method

In this example, I've used the filter() method to create a new array filled with all elements that pass the test implemented by the provided function.

const array1 = ['🟦', 'πŸ”΅', '🟦', '🟦'];

const array2 = array1.filter((item) => item == '🟦');

console.log('array1: ' + array1);
console.log('array2: ' + array2);
Enter fullscreen mode Exit fullscreen mode

Output:

array1: 🟦,πŸ”΅,🟦,🟦
array2: 🟦,🟦,🟦
Enter fullscreen mode Exit fullscreen mode

3. find() method

In this example, I've used the find() method to get the first element in the provided array that satisfies the provided testing function.

const array = ['🟦', '🟦', 'πŸ”΅', 'πŸ”΅'];

const item = array.find((item) => item == 'πŸ”΅');

console.log('array: ' + array);
console.log('item: ' + item);
Enter fullscreen mode Exit fullscreen mode

Output:

array: 🟦,🟦,πŸ”΅,πŸ”΅
item: πŸ”΅
Enter fullscreen mode Exit fullscreen mode

πŸ“ Note:

If there's no value that satisfies the testing function, undefined is returned.

4. fill() method

In this example, I've used the fill() method to change all elements in an array to a specific value, from a start index 1 (default is 0) to an end index (default array.length).

const array = ['🟦', '🟦', '🟦', '🟦'];
console.log('array: ' + array);

array.fill('πŸ”΅', 1); // filling since index 1
console.log('array: ' + array);

array.fill('πŸ”΅');    // filling since index 0
console.log('array: ' + array);
Enter fullscreen mode Exit fullscreen mode

Output:

array: 🟦,🟦,🟦,🟦
array: 🟦,πŸ”΅,πŸ”΅,πŸ”΅
array: πŸ”΅,πŸ”΅,πŸ”΅,πŸ”΅
Enter fullscreen mode Exit fullscreen mode

5. some() method

In this example, I've used the some() method to test whether at least one element in the array passes the test implemented by the provided function. The method returns true if, in the array, it finds at least one element for which the provided function returns true.

const array = ['🟦', 'πŸ”΅', '🟦', 'πŸ”΅'];

const result = array.some((item) => item == '🟦');

console.log('array: ' + array);
console.log('result: ' + result);
Enter fullscreen mode Exit fullscreen mode

Output:

array: 🟦,πŸ”΅,🟦,πŸ”΅
result: true
Enter fullscreen mode Exit fullscreen mode

6. every() method

In this example, I've used the every() method to test whether all elements in the array pass the test implemented by the provided function. The method returns true or false.

const array = ['🟦', '🟦', '🟦', 'πŸ”΅'];

const result = array.every((item) => item == 'πŸ”΅');

console.log('array: ' + array);
console.log('result: ' + result);
Enter fullscreen mode Exit fullscreen mode

Output:

array: 🟦,🟦,🟦,πŸ”΅
result: false
Enter fullscreen mode Exit fullscreen mode

You can run these examples here

If you found this solution useful let me know in the comment section or just leave a reaction πŸ’—πŸ¦„πŸ’Ύ.
Thanks for reading and see you in the upcoming posts! πŸ˜ŠπŸ”œ


Write to us! βœ‰

If you have any problem to solve or questions that no one can answer related to a React or JavaScript topic, or you're looking for a mentoring write to us on dirask.com -> Questions

Top comments (0)