[JavaScript] How to print even elements from an array?


I want to print only elements which are in even indexes, how to do that in Javascript?

1 Answer

3 years ago by

You can use filter function like below to print only the elements with even indices

  let numbers = ['zero','one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
  console.log(numbers.filter(ele => numbers.indexOf(ele) % 2 == 0))

Try checking the result here: https://onecompiler.com/javascript/3xmt8dfck

3 years ago by Meera