В JS функцията за обратно извикване е функция, която се предава на друга функция или метод като аргумент. Функцията за обратно извикване не се извиква автоматично. Извиква се при необходимост. Когато се използва функция за обратно извикване, нейните скоби се премахват. Ако се добавят скоби, функцията ще бъде извикана автоматично. напр

Пример по-долу е проста манипулация на DOM чрез функция за обратно извикване

Създайте скрипт файл във вашия html документ или външен JavaScript файл и добавете кода по-долу към него.

/* get the first h1 element from our html document page and save it as firstTitleElement */
const firstTitleElement = document.querySelector(‘h1’);
/* create a callback function that will change the background color of the first h1 element when the user click on it */
function exampleCallBackFunction(){
       firstTitleElement.style.background = 'blue';
}
/* now set an event listener that will call the exampleCallBackFunction when the user click on the h1 element*/
firstTitleElement.addEventListener('click',exampleCallBackFunction);
/* NOTE: when we used the callback function, we didn't add parenthesis to it like this exampleCallBackFunction(). If we had added parenthesis to it, the background colour would have changed to blue before we attempt clicking on the element */