DEV Community

Zeeshan Ahmd
Zeeshan Ahmd

Posted on

4

JavaScript array methods

There are many javascript array mehthods but we will discuss only the following:

  • Map
  • Reduce
  • Filter
  • Some
  • Every

Map

This method will executes the callback function on each element of the array.

array.map((item, index, array) => {}, thisValue)
Enter fullscreen mode Exit fullscreen mode

Now lets have a look at the usage of the map method:

const names = ["John", "Jane"];
const greetings = names.map((name, index) => {
    console.log(index);
    return `Hi, ${name}`;
});
console.log(greetings);
Enter fullscreen mode Exit fullscreen mode

Executing above code will output the following:

0, 1
[ "Hi, John", "Hi, Jane" ]
Enter fullscreen mode Exit fullscreen mode

Reduce

This method will reduce the array into one element.

const numbers = [1, 2, 3];
const total = numbers.reduce((total, currentValue) => total + currentValue);
console.log(total);
Enter fullscreen mode Exit fullscreen mode

Executing above code will output the following:

6
Enter fullscreen mode Exit fullscreen mode

Filter

This method will extract all the elements from the array which passed the provided condition inside call back function.

const users = [
  {
    name: 'John',
    isActive: true,
  },
  {
    name: 'Alice',
    isActive: false,
  },
  {
    name: 'Bob',
    isActive: true,
  },
];

const activeUsers = users.filter(user => user.isActive);
console.log(activeUsers);
Enter fullscreen mode Exit fullscreen mode

Executing above code will output the following:

[
  {
    "name": "John",
    "isActive": true
  },
  {
    "name": "Bob",
    "isActive": true
  }
]
Enter fullscreen mode Exit fullscreen mode

Some

This method will return true if one element passed the condition otherwise it will return false.

const users = [
  {
    name: 'John',
    isActive: false,
  },
  {
    name: 'Alice',
    isActive: false,
  },
  {
    name: 'Bob',
    isActive: true,
  },
];

const isOneUserActive = users.some(user => user.isActive === true);
console.log(isOneUserActive);
Enter fullscreen mode Exit fullscreen mode

Executing above code will output the following:

true
Enter fullscreen mode Exit fullscreen mode

Every

This method will return true if all elements passed the condition provided inside callback function otherwise it will return false.

const users = [
  {
    name: 'John',
    isActive: false,
  },
  {
    name: 'Alice',
    isActive: true,
  },
  {
    name: 'Bob',
    isActive: true,
  },
];

const isAllUserActive = users.every(user => user.isActive === true);
console.log(isAllUserActive);
Enter fullscreen mode Exit fullscreen mode

Executing above code will output the following:

false
Enter fullscreen mode Exit fullscreen mode

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Jetbrains Survey

Calling all developers!

Participate in the Developer Ecosystem Survey 2025 and get the chance to win a MacBook Pro, an iPhone 16, or other exciting prizes. Contribute to our research on the development landscape.

Take the survey

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️