Why does filter removes index 0 from my array

I just wanted to confirm something that’s bugging me.

I’m still learning javascript and did a challenge today. The challenge was to remove the value of every 2nd index.

I had to use the filter as part of the challenge and realised index2 on the code below doesn’t return index 0 from the original array chopping off the value “a”. So I got it to work by adding 1 to each of the indexes (index2 +1). Does it remove the 0 because filter() always returns true and 0 is seen as false? Sorry I know it’s probably a simple answer and thanks to anyone for taking the time to help me.

const nums = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
const index = 3;

const extractEachKth=(nums, index)=>nums.filter((item,index2)=>(index2 +1)%index!==0)

expected result ["a", "b", "d", "e", "g", "h", "j"]```


This statement is wrong, filter() always returns true. Rather filter will let those values pass the filter for which we are returning true. In your case you are returning true if (index2 +1)%index!==0.

When index2 == 0, (index2 +1)%index!==0 => (0+1)%3 !== 0 => 1 !== 0 which is true. Therefore it lets the 0th item from the array to be passed through the filter.

Before when you weren’t adding +1 to the index2,

When index2 == 0, index2%index!==0 => 0%3 !== 0 => 0!== 0 which is false. Therefore it filtered the 0th item and you didn’t get that item.