JavaScript Reference
A quick reference for JavaScript keywords, functions, and objects. Find what you need to build dynamic web applications.
The `function` Keyword
The function keyword is used to define a function in JavaScript. Functions are one of the fundamental building blocks in JavaScript, a set of statements that performs a task or calculates a value.
To use a function, you must define it somewhere in the scope from which you wish to call it.
Example
functions.js
// Function declaration
function greet(name) {
return "Hello, " + name + "!";
}
// Function expression
const farewell = function(name) {
return "Goodbye, " + name + "!";
};
// Arrow function
const double = (x) => x * 2;
console.log(greet("World")); // Output: Hello, World!
console.log(double(5)); // Output: 10Syntax
Ways to define a function:
| Syntax | Description |
|---|---|
| function functionName(parameters) { ... } | The standard function declaration. |
| const functionName = function(parameters) { ... } | A function expression, often assigned to a variable. |
| const functionName = (parameters) => { ... } | An arrow function expression, providing a shorter syntax. |