Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

JavaScript Functions

The document provides an overview of different types of JavaScript functions, including anonymous functions, regular functions, arrow functions, immediately invoked function expressions (IIFE), callback functions, and higher-order functions. Each function type is described with its use cases and examples. Additionally, a summary table outlines when to use each function type for clarity.

Uploaded by

unknowndijago
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JavaScript Functions

The document provides an overview of different types of JavaScript functions, including anonymous functions, regular functions, arrow functions, immediately invoked function expressions (IIFE), callback functions, and higher-order functions. Each function type is described with its use cases and examples. Additionally, a summary table outlines when to use each function type for clarity.

Uploaded by

unknowndijago
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

01

JAVASCRIPT
Functions
Master JavaScript Functions - The
Ultimate Guide!
02

ANONYMOUS FUNCTIONS

A function without a name, stored inside a


variable.

Use Case:
Used in callbacks, event handlers, and
function expressions.
Not reusable unless stored in a variable.

const multiplyByTwo = function(num) {


return num * 2;
};
console.log(multiplyByTwo(5)); // Output: 10
03

REGULAR FUNCTIONS

A traditional function that gets hoisted to


the top, meaning you can call it before
declaring it!

Use Case:
Best for reusable logic in applications.
Improves code readability and
debugging.

function multiplyByTwo(num) {
return num * 2;
}
console.log(multiplyByTwo(5)); // Output: 10
04

ARROW FUNCTIONS(ES6+)

A modern, shorter syntax for functions.

Use Case:
Used in short, single-expression
functions.
Great for array methods (map, filter,
reduce).

const multiplyByTwo = (num) => num * 2;


console.log(multiplyByTwo(5)); // Output: 10

Note: Arrow functions don’t have their


own this keyword.
05

IMMEDIATELY INVOKED
FUNCTION EXPRESSION (IIFE)

A function that executes immediately


after creation.

Use Case:
Used for avoiding global scope
pollution.
Often used in modular JavaScript &
closures.

console.log((function(num) {
return num * 2;
})(5)); // Output: 10
06

CALLBACK FUNCTIONS

A function passed as an argument to


another function.

Use Case:
Used in event handling, API calls, and
async operations.
Makes JavaScript non-blocking.

function processNumber(num, callback) {


return callback(num);
}
console.log(processNumber(5, function(n) {
return n * 2; })); // Output: 10
07

HIGHER-ORDER FUNCTIONS

A function that takes another function as


an argument or returns a function.

Use Case:
Used in functional programming & code
abstraction.
Helps in creating reusable logic.

function multiplier(factor) {
return function(num) {
return num * factor;
};
}

const multiplyByTwo = multiplier(2);


console.log(multiplyByTwo(5)); // Output: 10
08

SUMMARY:
WHEN TO USE EACH FUNCTION?

Function Type Use Case

Anonymous
One-time use, event handlers
Function

Named Function Reusable code, debugging

Arrow Function Short functions, array methods

Runs immediately, avoids global


IIFE
variables

Callback Function Asynchronous code, event handling

Higher-Order Functional programming, reusable


Function utilities
09

REPOST,
IF YOU LIKED THE POST!
Save this for later & Share with your fellow
developers & let’s level up together!

You might also like