Function Declaration
Last Updated: 21st October 2025
There are different way to declare a function in JavaScript.Every Function can have a name, parameters, and a body.
- functionName → Name of the function
- parameters → Optional values you can pass into the function
- Code inside → Runs when the function is called
Here we see some of them without parameters.
Function Declaration
- Function Declaration is a named function that is defined using the
functionkeyword. - It is hoisted, which means it can be called before it is defined in the code.
Hinglish Tip 🗣: Function Declaration ka matlab hai “pehle function define karo aur fir kahin bhi call kar sakte ho”.
function functionName() {
// function body
}
Example:
function greet() {
console.log("Hello, world!");
}
greet(); // Output: "Hello, world!"
Function Expression or Anonymous Function
- A function expression is a function stored in a variable.
- Unlike function declaration, it is not hoisted → you cannot call it before definition.
- Can be named or anonymous.
Hinglish Tip 🗣: Function Expression = “Function ko variable me store karna”. Call tabhi ho sakta hai jab variable define ho chuka ho.
// Anonymous function expression
const functionName = function (parameters) {
// code block
};
// Named function expression
const functionName = function actualName(parameters) {
// code block
};
Example:
const greet = function () {
console.log("Hello, world!");
};
greet(); // Output: "Hello, world!"
Arrow Function
- Arrow Functions are a shorter syntax to write functions in JavaScript.
- Introduced in ES6 (ECMAScript 2015).
- They are anonymous (no name) and often used as callback functions or inside array methods like
map(),filter(),forEach(), etc.
Hinglish Tip 🗣: Arrow function ek chhota shortcut hai — likhne me easy aur modern JavaScript me bahut use hota hai.
const functionName = () => {
// code block
};
Example:
const greet = () => {
console.log("Hello, world!");
};
greet(); // Output: "Hello, world!"