Sign in
Log inSign up

A weird example of filter method in JavaScript

Default profile photo
Anonymous
·Apr 9, 2018
function CheckValue(value, index, ar) {  
    if (index == 0)  
        return true;  
    else  
        return ar[index - 1] === " ";  
}  

// Create a string.  
var sentence = "The quick brown fox jumps over the lazy dog.";   

// Create an array that contains all characters that follow a space.  
var subset = [].filter.call(sentence, CheckValue);   


document.write(subset);

As per the official doc, I have seen filter method is called upon an array. But here things seem to be messed up. For the first time, I have seen this kind of example using filter method.

I am unable to understand that how this code is working under the hood.