π JavaScript: Functions, Callback Functions & Arrow Functions

Full Stack Developer Intern at DevSync
Presented by Swapnil Meshram within mentorship Devsync
π Started learning JavaScript with the guidance of Devsync, focusing on basic concepts of functions, callback functions, and arrow functions with simple hands-on practice.
π 1. JavaScript Functions
π· What is a function?
A function in JavaScript is a reusable block of code that performs a specific task. Functions help organize code, make it readable, and reduce repetition.
You can define a function once and call it multiple times wherever needed.
πΉ Function Declaration
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Alice"); // Output: Hello, Alice!
β Explanation:
functionis the keyword to declare a function.greetis the name of the function.nameis the parameter.The function logs a greeting to the console.
greet("Alice")calls the function and passes it"Alice"as an argument.
πΉ Function with Return Value
function add(a, b) {
return a + b;
}
let result = add(5, 3);
console.log(result); // Output: 8
β Explanation:
The
returnkeyword sends back a result from the function.This allows you to store or use the result somewhere else.
π 2. Callback Functions
π· What is a callback function?
A callback function is a function that is passed as an argument to another function and is executed later inside the parent function.
This is very useful in asynchronous operations like loading data, waiting for a response, etc.
πΉ Example:
function greetUser(name, callback) {
console.log("Hi, " + name);
callback();
}
function sayBye() {
console.log("Goodbye!");
}
greetUser("John", sayBye);
/*
Output:
Hi, John
Goodbye!
*/
β Explanation:
greetUsertakes an and an asnamecallbackparameters.After greeting the user, it calls the
callbackfunction.sayByeis passed in and executed after the greeting.
πΉ Real-life Analogy:
Imagine you're ordering food:
You place an order (main function).
The chef will call you back when itβs ready.
π 3. Arrow Functions
π· What is an arrow function?
Arrow functions are a shorter way to write functions in JavaScript. They were introduced in ES6 (ECMAScript 2015).
They are often used in situations where you need simple functions quicklyβlike callbacks, array methods, etc.
πΉ Basic Arrow Function Syntax
const add = (a, b) => {
return a + b;
};
console.log(add(2, 3)); // Output: 5
β Explanation:
const add = (a, b) => { ... }defines a function using arrow syntax.The
=>replaces thefunctionkeyword.The function still works the same but looks cleaner.
πΉ Arrow Function with One-Line Return
const square = num => num * num;
console.log(square(4)); // Output: 16
β Explanation:
If there's only one expression, you can skip the
{}andreturn.It will return the value automatically.
πΉ Arrow Function with No Parameters
const greet = () => console.log("Hello World!");
greet(); // Output: Hello World!
β Explanation:
- Use empty
()if there are no parameters.
πΉ Difference
| Type | Description | Syntax Example |
| Regular Function | Reusable block of code | function greet() {} |
| Callback | Function passed as argument | doSomething(name, callback) |
| Arrow Function | Short and clean function syntax (ES6) | const sum = (a, b) => a + b; |
β Final Summary
| Concept | Description | Example |
| Function | Reusable block of code | function greet() {} |
| Callback Function | Function passed and called inside another function | greet(name, callback) |
| Arrow Function | Shorter function syntax from ES6 | const sum = (a, b) => a + b |
π Tip:
Functions are one of the most powerful parts of JavaScript. Mastering them will help you write clean, maintainable, and reusable code.