Menu
Try in Playground

JavaScript Function Declarations: Syntax, Hoisting, and Return

How to declare functions in JavaScript — the function keyword, parameters, return values, hoisting, and when to reach for a declaration vs an expression.

A Function Is a Named Block of Steps

Any time you want to give a chunk of logic a name and reuse it, you write a function. The oldest and most straightforward way in JavaScript is the function declaration — the function keyword followed by a name.

index.js
Output
Click Run to see the output here.

Reading that left to right:

  • function is the keyword that starts a declaration.
  • greet is the function name.
  • (name) is the parameter list — the inputs the function takes.
  • The block in braces is the body.
  • greet("Ada") is a call. JavaScript runs the body with name bound to "Ada".

Defining a function doesn't run it. Calling it does.

Parameters and Arguments

The word parameter is the name inside the function definition. The word argument is the value passed in when you call. Casual speech treats them as synonyms; the distinction matters when you're reading error messages.

index.js
Output
Click Run to see the output here.

Here base and exponent are parameters. 2 and 10 are arguments. JavaScript binds them in order: first argument to first parameter, and so on.

Unlike some languages, JavaScript won't complain if you pass the wrong number of arguments. Missing ones become undefined; extras are silently ignored. That's flexible and occasionally a trap — we'll see defaults and rest parameters in later docs.

return Sends a Value Back

console.log writes to the output. return hands a value back to the caller so they can use it:

index.js
Output
Click Run to see the output here.

Without return, a function returns undefined:

index.js
Output
Click Run to see the output here.

return also exits the function immediately. Early returns are the standard way to bail on edge cases before the main logic:

index.js
Output
Click Run to see the output here.

Early returns keep the main body from turning into a pyramid of if/else.

Hoisting: You Can Call It Before You Define It

Here's something that genuinely surprises people coming from other languages. Function declarations are hoisted — JavaScript moves them to the top of their scope before running any code. That means you can call a function on a line above where it's declared:

index.js
Output
Click Run to see the output here.

That code runs fine. Before execution, the engine scans the scope, registers square as a function, and only then starts running statements top to bottom.

This is a real behavioral difference between function declarations and the other ways to make a function (function expressions, arrow functions). Those get hoisted like variables — the name is known, but the value isn't assigned until the line runs. More on that distinction next.

Most style guides recommend defining functions before you call them anyway. Hoisting is a safety net, not a style to lean on.

Declarations vs Expressions

A function declaration stands on its own as a statement. A function expression appears where a value is expected — most often on the right-hand side of an assignment:

index.js
Output
Click Run to see the output here.

Both produce callable functions. The differences:

  • Declarations are hoisted in full. Expressions follow the hoisting rules of the variable they're assigned to (const and let are in the temporal dead zone until the line runs).
  • Declarations must have a name. Expressions can be anonymous, though naming them helps stack traces.
  • Declarations can't appear just anywhere. In strict mode, a function foo() {} inside an if block behaves inconsistently across engines — use an expression there instead.

For most top-level helpers in a file, a declaration reads well. For functions passed as arguments or assigned to properties, an expression (or an arrow function) is the usual choice.

Naming and Style

A function name is a promise about what the function does. Spending a few extra characters to pick a descriptive one pays for itself the first time someone reads the code.

index.js
Output
Click Run to see the output here.

A few conventions worth adopting:

  • Function names should be verbs: fetchProfile, computeTotal, sendEmail.
  • Use camelCase — that's the JavaScript norm.
  • Boolean-returning functions often start with is, has, or can: isValid, hasAccess, canEdit.

The goal is for the call site to read like a sentence.

A Small Working Example

Pulling the pieces together — parameters, early return, a descriptive name:

index.js
Output
Click Run to see the output here.

One declaration, a guard clause, a clear return. That shape covers most of the functions you'll write.

Next: Arrow Functions

Declarations are the workhorse, but modern JavaScript leans heavily on a shorter syntax — arrow functions — especially for callbacks and one-liners. They look different, they behave differently around this, and they're what the next page is about.

Frequently Asked Questions

How do you declare a function in JavaScript?

Use the function keyword, followed by a name, parentheses for parameters, and a block. Example: function greet(name) { return 'Hi, ' + name; }. You call it by writing its name with parentheses: greet('Ada').

What is function hoisting in JavaScript?

Function declarations are moved to the top of their enclosing scope before the code runs, so you can call them on a line above where they're written. This only applies to function declarations — function expressions and arrow functions assigned to let or const are not hoisted the same way.

What's the difference between a function declaration and a function expression?

A declaration stands alone as a statement (function greet() {}) and is hoisted. An expression assigns a function to a variable (const greet = function() {}) and follows the usual variable hoisting rules. Declarations are named by definition; expressions are often anonymous.

Learn to code with Coddy

GET STARTED