Function
In one line
A function is a reusable block of code that takes inputs (parameters), performs a task, and optionally returns a value.
In simple words
A function is a named, reusable block of code. You define it once and call it many times with different inputs. Functions are the primary way to organize and reuse logic in JavaScript.
Functions can be declared several ways: function declarations (function foo() {}), function expressions (const foo = function() {}), and arrow functions (const foo = () => {}). Each has slightly different behavior regarding hoisting and this binding.
Functions are first-class in JavaScript — they can be assigned to variables, passed as arguments, and returned from other functions. This makes higher-order functions, callbacks, and functional programming patterns possible.
Example
function add(a, b) {
return a + b;
}
const multiply = (a, b) => a * b;
console.log(add(2, 3)); // 5
console.log(multiply(4, 5)); // 20A function declaration and an arrow function expression — both are functions.
Related terms
Related Resources
Related Lessons
- Functions
Learn about functions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.