function keyword
Starts a function declaration.
Function declarations create named reusable blocks of behavior. They are excellent for important actions that deserve a clear name.
function formatTitle(value) {
return value.trim().toUpperCase();
}
console.log(formatTitle(" JavaScript "));
Functions & Scope
A function declaration starts with the function keyword, followed by a name, parentheses and a block. The name should explain what the function does.
Function declarations are hoisted. That means the function can be called before the declaration appears in the file. This is useful, but code is still easier to read when important functions are near the code that uses them.
Use declarations for behavior that is central to the module: validation, formatting, rendering, calculation or a named operation that will be reused.
Starts a function declaration.
Communicates the action or result.
Inputs listed inside parentheses.
Statements inside curly braces that run when the function is called.
Examples
function createStatusLabel(status) {
return status === "ready" ? "Ready" : "Not ready";
}
const label = createStatusLabel("ready");
function doStuff(x) {
return x.trim().toUpperCase();
}
// The function works, but the name hides the purpose.
Code patterns
These examples focus on syntax you will actually look up later: declarations, callbacks, returns, closures and context handling. Use the small patterns first, then study the deeper explanation.
Name the action and call it later.
function greet() {
return "Hello";
}
console.log(greet());
Pass input into the function.
function upper(value) {
return value.toUpperCase();
}
console.log(upper("ready"));
Return early when input is invalid.
function isValidScore(score) {
if (!Number.isFinite(score)) {
return false;
}
return score >= 0 && score <= 100;
}
Keep UI updates small and named.
function renderMessage(status) {
return status === "ready" ? "Ready" : "Waiting";
}
output.textContent = renderMessage("ready");
Rules that matter
Functions are where JavaScript stops being a list of statements and becomes a system. Scope decides what each function can see, while return values and parameters decide how functions communicate.
formatTitle, validateInput and renderList are clearer than doStuff.
A function should do one coherent job.
A reusable function is easier to test when it returns a result.
Pass values in through parameters when possible.
Guard clauses keep the main path readable.
Hoisting is not an excuse for a confusing file.
Production thinking
Functions are how code becomes organized. Without clear functions, programs become long timelines with repeated behavior scattered everywhere.
Named functions make it easier to keep accessible UI behavior consistent, such as message rendering and focus handling.
Production code should use small named functions for reusable business logic, formatting and DOM behavior.
Rendering functions that create headings, links or metadata should be predictable, named and easy to test.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Functions only become useful when you can explain what goes in, what comes out and which scope the function can see.