Javascript Array filter() get all numbers

Introduction

Refactor the code below using filter().

var numbers = [1, 2, 4, 2];
var evenNumbers = [];
for (var i = 0; i < numbers.length; i++) {
   if (numbers[i] % 2 === 0) {
     evenNumbers.push(numbers[i]);
   }
}
console.log(evenNumbers);

var numbers = [1, 2, 4, 2];

var evenNumbers = numbers.filter(function(number) {
  return number % 2 === 0;
});//from  ww  w. j  a  va 2  s  .  com

console.log(evenNumbers);



PreviousNext

Related