JavaScript - Functions
A JavaScript function is a reusable block of code that accepts inputs (arguments), processes them, and returns a value. Functions are declared using the "function" keyword, followed by a name and a set of parentheses that may include parameters. Functions are executed by calling the function name and passing in arguments.
Example:
function greeting(name) {
return "Hello, " + name;
}
let greet = greeting("John");
console.log(greet); // Output: "Hello, John"
Functions can also be defined using anonymous functions, known as function expressions.
Example:
let greet = function (name) {
return "Hello, " + name;
};
console.log(greet("John")); // Output: "Hello, John"
JavaScript functions can be used to simplify complex logic, to reduce code duplication, and to break down a large codebase into smaller, manageable parts.