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.
const functionName = function(parameters) {
// Function body
};
// Or with arrow function syntax
const functionName = (parameters) => {
// Function body
};
const greet = function(name) {
return `Hello, ${name}!`;
};
// Calling the function
console.log(greet('Prathamesh')); // Output: Hello, Prathamesh!
const greet = (name) => `Hello, ${name}!`;
// Calling the function
console.log(greet('Prathamesh')); // Output: Hello, Prathamesh!
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!');
})();