Lecture

Advanced Features of Functions

Let's dive into more advanced techniques for using functions.


Callback Functions

A callback function is a function that is passed as an argument to another function.

Invoking a Passed Callback Function
function study(callback) { console.log('Studying...'); callback(); // call the callback function } function call() { console.log('Callback function called!'); } study(call);

In the above code, the study function takes the call function as an argument and executes it.

Here, the call function is the callback function of the study function.

When executing study(call), the call function is invoked, outputting Studying... and Callback function called!.


Recursive Function

A recursive function is a function that calls itself.

Implementing Factorial Using a Recursive Function
function factorial(n) { if (n === 1) return 1; return n * factorial(n - 1); } console.log(factorial(5)); // Outputs 120

IIFE (Immediately Invoked Function Expression)

An IIFE is a pattern for defining and immediately invoking a function.

Example Using IIFE
(function () { console.log('This function is executed immediately!'); })();

Built-in JavaScript Functions

JavaScript provides several useful built-in functions, such as those for sorting arrays or slicing strings.

Functions provided by JavaScript itself are called built-in functions.

For example, operations like sorting an array or slicing a string are supported by basic functions.

String Slicing
let str = 'Hello, world!'; let slicedStr = str.slice(0, 5); console.log(slicedStr); // Outputs "Hello"
Mission
0 / 1

A callback function is a function that is passed as an argument to another function.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run

Execution Result