DEV Community

Khadija Batool Gardezi
Khadija Batool Gardezi

Posted on

Callback function in js?🧐

So basically, a callback function is a function() passed as an argument to another function. It is executed for each element in the array during iteration.

Example:
recipe => recipe.name.toLowerCase().includes(filter.toLowerCase())

Method: .filter()
Loops through each item in the recipes array and runs the callback function to decide if that item should be included in the filtered results.

Callback Function:
recipe => recipe.name.toLowerCase().includes(filter.toLowerCase())
Parameters:
recipe: Represents the current element in the recipe array.

recipe.name.toLowerCase(): Converts the recipe's name to lowercase for case-insensitive comparison.

.includes(filter.toLowerCase()): Checks if the lowercase filter string is part of the recipe's name.

The arrow function (recipe => recipe.name...) is the callback here.

Top comments (0)