what is array filter() in JavaScript

filter() run a function on every element in an array and keeps the element if the function returned a truthy value otherwise remove them. The original array isn’t modified.

The function passed into filter() takes in these parameters:

An example could be removing duplicates.

JavaScript

const arrayWithDuplicates = [1, 1, 2, 2, 3, 3, 4, 4];

const arrayWithoutDuplicates = arrayWithDuplicates.filter(
    (number, index, array) => {
        return index !== array.indexOf(number);
    }
);

console.log(arrayWithDuplicates); // [1, 1, 2, 2, 3, 3, 4, 4]
console.log(arrayWithoutDuplicates); // [1, 2, 3, 4];

What is happening??

The logic is checking if the index of the current element is not the same as the index where it was first seen.

summary of filter()

filter() is used to filter out element in an array.

html
css
javascript
Initializing Git

Git is a tool that we can use to save all changes and additions to our code on the computer we're working on.

html
css
javascript
Tracking changes with Git

Now that we know how to setup Git, lets see how we can track changes.