How to Define a Function in JavaScript
A function is a named piece of code you can run as often as you like. The most direct way to write one in JavaScript is a function declaration: the word function, a name, the parameters in brackets and the body in braces.
Syntax
function <name>(<parameter>, <parameter>) {
return <value>;
}
<name>(<argument>, <argument>);
| Part | What it does |
|---|---|
function <name> | Declares a function and gives it a name. |
(<parameter>, <parameter>) | Names for the values the function receives. A function can have none. |
return <value> | Ends the function and hands <value> back to the code that called it. |
<name>(<argument>, <argument>) | Calls the function, with a value for each parameter. |
MDN's function declaration reference: "The function declaration creates a binding of a new function to a given name." And: "Each time when a function is called, it returns the value specified by the last executed return statement, or undefined if the end of the function body is reached."
The Example
The example declares two functions, square and greet. It calls square on the line before its declaration, then calls greet once with a name and once without one.
Prerequisites
- Node.js. This guide uses Node.js 26.13.1.
Step 1: Write the Code
Create a file named main.js:

console.log(square(4));
function square(n) {
return n * n;
}
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet('Ada'));
console.log(greet());
Step 2: Run It
node main.js
16
Hello, Ada!
Hello, undefined!

The command is the same in zsh, bash and PowerShell.
What Happened
square(4)ran on the line above the declaration ofsquareand still printed16. MDN explains why: "function declarations are hoisted together with their values and can be called anywhere in their scopes."greet('Ada')passed'Ada'asname, andreturnhanded back the finished text.greet()passed nothing, sonamewasundefined, and that word ended up in the text.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out return | The call gives back undefined: a function whose body was only n * 2; returned undefined | Put return before the value |
Calling a function kept in a const above its line | ReferenceError: Cannot access 'f' before initialization | Call it below the line, or write it as a function declaration |
| Leaving out an argument | The parameter is undefined | Pass it, or give the parameter a default value: with function greet(name = 'there'), greet() returned Hello, there! |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
undefined where you expected a value | The function has no return, or an argument is missing | Add the return, or pass the argument |
ReferenceError: grett is not defined | The name in the call is misspelled | Use the exact name from the declaration |
ReferenceError: Cannot access 'f' before initialization | A function stored in a const or let was called before that line | Move the call below the line |
FAQ
What's the difference between a function declaration and a function expression?
A declaration is a statement that starts with function, and it's hoisted. An expression makes a function as a value, often kept in a variable, as in const double = function (n) { return n * 2; };. That variable exists only from its line on, which is why calling it earlier fails.
Does a function need parameters?
No. function now() { return Date.now(); } takes none, and you call it with empty brackets: now().
Conclusion
function name(parameters) { ... } declares a function, and return hands its result back. Declared functions can be called anywhere in their scope, even above their own line, and any argument you leave out arrives as undefined.
