JS Functions
A JavaScript function is a block of code designed to perform a particular task.
A function is defined once and can be called (invoked) as many times as needed. This prevents code repetition and keeps your programs organised.
Syntax:
function functionName(parameter1, parameter2) {
// code to run
return result;
}
Try It — Functions in Action
Preview
Key Concepts
| Concept | Description | Example |
|---|---|---|
| Definition | Declare the function | function add(a, b) { ... } |
| Parameter | Input the function receives | a and b above |
| Return | Value the function sends back | return a + b; |
| Invocation | Calling the function | add(3, 5) → 8 |
Tip: Functions that return a value can be used directly in expressions:
let result = multiply(4, 3) + 10;