Skip to main content

How to Define a Function in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
function <name>(<parameter>, <parameter>) {
return <value>;
}

<name>(<argument>, <argument>);
PartWhat 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:

main.js, which logs square of 4 before declaring square, declares square and greet, then logs greet with Ada and greet with no argument.

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​

Terminal
node main.js
Output
16
Hello, Ada!
Hello, undefined!

A zsh terminal where node main.js prints 16, Hello, Ada! and Hello, undefined!

The command is the same in zsh, bash and PowerShell.

What Happened​

  • square(4) ran on the line above the declaration of square and still printed 16. MDN explains why: "function declarations are hoisted together with their values and can be called anywhere in their scopes."
  • greet('Ada') passed 'Ada' as name, and return handed back the finished text.
  • greet() passed nothing, so name was undefined, and that word ended up in the text.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out returnThe call gives back undefined: a function whose body was only n * 2; returned undefinedPut return before the value
Calling a function kept in a const above its lineReferenceError: Cannot access 'f' before initializationCall it below the line, or write it as a function declaration
Leaving out an argumentThe parameter is undefinedPass it, or give the parameter a default value: with function greet(name = 'there'), greet() returned Hello, there!

Troubleshooting​

SymptomLikely causeFix
undefined where you expected a valueThe function has no return, or an argument is missingAdd the return, or pass the argument
ReferenceError: grett is not definedThe name in the call is misspelledUse the exact name from the declaration
ReferenceError: Cannot access 'f' before initializationA function stored in a const or let was called before that lineMove 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.

Additional Resources​