Definition

A function expression defines a function inside an expression, and it can be named or anonymous. The function can only be called after it is defined.

Syntax

const functionName = function(parameters) {
    // Function body
};

// Or with arrow function syntax
const functionName = (parameters) => {
    // Function body
};

Example

const greet = function(name) {
    return `Hello, ${name}!`;
};

// Calling the function
console.log(greet('Prathamesh')); // Output: Hello, Prathamesh!

Example with Arrow Function

const greet = (name) => `Hello, ${name}!`;

// Calling the function
console.log(greet('Prathamesh')); // Output: Hello, Prathamesh!

Use Cases

const numbers = [1, 2, 3, 4, 5];

const doubled = numbers.map(function(num) {
    return num * 2;
});

console.log(doubled); // Output: [2, 4, 6, 8, 10]

(function() {
    console.log('This is an IIFE!');
})();