Javascript function
1.Function Declaration
function functionName(parameters) {
// Code to be executed
return value; // Optional
}
Example:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Output: Hello, Alice!
2.Function Expression
const functionName = function(parameters) {
// Code to be executed
return value; // Optional
};
Example:
const add = function(a, b) {
return a + b;
};
console.log(add(5, 3)); // Output: 8
3.Arrow Function (ES6)
const functionName = (parameters) => {
// Code to be executed
return value; // Optional
};
Example:
const multiply = (a, b) => a * b;
console.log(multiply(4, 2)); // Output: 8