function
Try it yourself →Defines a function.
Syntax
function name(param) { return value; }Description
Binds repetitive work to a name; takes data via parameters and returns a result with return.
Example
function square(x) {
return x * x;
}
console.log(square(5));Expected output
25
When to use?
Preventing code repetition, breaking the program into parts.
Common mistakes
- ⚠️ If you don't write return, the function returns undefined.