Completion requirements
function keyword
Examples
Creating an unnamed function
The following example defines an unnamed function and assigns it to x
. The function returns the square of its argument:
const x = function (y) { return y * y; };
Using a function as a callback
More commonly it is used as a callback:
button.addEventListener('click', function (event) { console.log('button is clicked!'); })
Using an Immediately Invoked Function Expression (IIFE)
An anonymous function is created and called:
(function () { console.log('Code runs!'); })(); // or !function () { console.log('Code runs!'); }();