How to Declare and Call a JavaScript Function

Learn the three ways to declare a function in JavaScript and how to call each one. From function declarations to arrow functions, with clear examples and common mistakes.

6 min read

Declaring a function is like writing a recipe. Calling a function is like cooking from that recipe. You need both -- the definition and the invocation -- for anything to happen.

JavaScript gives you three main syntaxes for declaring functions. Each looks slightly different and behaves slightly differently, but they all create the same thing: a reusable, callable block of code.

Function Declarations

A function declaration is the classic, most readable way to create a named function. It starts with the function keyword, followed by a name, a parameter list, and a body wrapped in curly braces:

javascriptjavascript
function functionName(parameter1, parameter2) {
  // code to run
  return value;
}

Here is a real example built from that shape. It takes one parameter, a name, and returns a greeting string built around it, so calling it with different names produces different results:

javascriptjavascript
function greetUser(name) {
  return `Hello, ${name}!`;
}

The name parameter is a placeholder here, it only becomes a real value once you call the function. To call it, write the function name followed by parentheses containing the arguments:

javascriptjavascript
console.log(greetUser("Sara")); // "Hello, Sara!"
console.log(greetUser("Alex")); // "Hello, Alex!"

Every call runs the function body fresh with the new argument.

Function declarations have one special behavior: hoisting. JavaScript moves all function declarations to the top of their scope before any code runs. This means you can call a function declaration before the line where it appears:

javascriptjavascript
console.log(square(5)); // 25 -- works fine
 
function square(n) {
  return n * n;
}

Hoisting is convenient, but it is specific to function declarations. Expressions and arrow functions do not hoist.

Function Expressions

A function expression assigns an anonymous function to a variable:

javascriptjavascript
const functionName = function(parameter1, parameter2) {
  // code to run
  return value;
};

Here the function has no name of its own. It exists only because it is assigned to the calculateTax variable, and that variable is what you call:

javascriptjavascript
const calculateTax = function(amount, rate) {
  return amount * rate;
};
 
console.log(calculateTax(100, 0.08)); // 8

The key difference from declarations: function expressions are not hoisted. The variable exists from the top of the scope, but the function is not assigned to it until that line executes.

javascriptjavascript
console.log(double(5)); // ReferenceError: Cannot access 'double' before initialization
 
const double = function(n) {
  return n * 2;
};

Use a function expression when you need to assign a function conditionally, pass it as an argument, or when the function only makes sense in one place.

Arrow Functions

Arrow functions are a shorter syntax introduced in ES6:

javascriptjavascript
const functionName = (parameter1, parameter2) => {
  // code to run
  return value;
};

If the body is a single expression, you can drop the curly braces and the return keyword. JavaScript treats the expression after the arrow as the return value automatically:

javascriptjavascript
const add = (a, b) => a + b;
const greet = (name) => `Hello, ${name}!`;

If there is exactly one parameter, you can even drop the parentheses around it, which shortens the syntax further for short callbacks:

javascriptjavascript
const double = n => n * 2;

Arrow functions are ideal for short callbacks and functional array methods, where a compact inline function reads more clearly than a full declaration:

javascriptjavascript
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(n => n * 2);
console.log(doubled); // [2, 4, 6, 8]

Arrow functions do not have their own this, arguments, or super. For a full comparison of all three declaration styles, see JavaScript Function Expressions vs Declarations and JavaScript Arrow Functions: A Complete ES6 Guide.

How to Call a Function

Calling a function is straightforward: write the name, then parentheses with arguments separated by commas.

javascriptjavascript
function displayInfo(name, age, city) {
  console.log(`${name} is ${age} and lives in ${city}.`);
}
 
displayInfo("Maya", 28, "Berlin");
// "Maya is 28 and lives in Berlin."

You can call a function as many times as you want. Each call is independent.

Calling with Fewer Arguments

If you pass fewer arguments than parameters, the missing ones become undefined:

javascriptjavascript
function showThree(a, b, c) {
  console.log(a, b, c);
}
 
showThree(1, 2);
// 1 2 undefined

No error is thrown. JavaScript is lenient about argument counts.

Calling with Extra Arguments

Extra arguments are silently ignored by the named parameters, but they still exist in the arguments object:

javascriptjavascript
function showFirst(a) {
  console.log(a);
  console.log(arguments);
}
 
showFirst(10, 20, 30);
// 10
// [Arguments] { '0': 10, '1': 20, '2': 30 }

Storing the Return Value

A function call is an expression that evaluates to its return value, so you can assign that result directly to a variable the same way you would assign any other value:

javascriptjavascript
function multiply(a, b) {
  return a * b;
}
 
const result = multiply(6, 7);
console.log(result); // 42

If there is no return statement, the call still evaluates to something, it just evaluates to undefined instead of a useful value:

javascriptjavascript
function logMessage(msg) {
  console.log(msg);
}
 
const val = logMessage("test"); // prints "test"
console.log(val);              // undefined

Calling a Function Inside Another Function

Functions can call other functions. This is how you compose logic: each small function handles one job, and a larger function combines their results into one final answer.

javascriptjavascript
function addTax(price) {
  return price * 1.08;
}
 
function formatPrice(price) {
  return `$${price.toFixed(2)}`;
}
 
function displayTotal(basePrice) {
  const total = addTax(basePrice);
  console.log(formatPrice(total));
}
 
displayTotal(100); // "$108.00"

Each function does one thing. displayTotal calls addTax and formatPrice to combine their results. This pattern is the backbone of clean code.

When to Use Each Declaration Style

StyleUse When
Function declarationYou want a named, hoisted function that is easy to find in stack traces
Function expressionYou need to assign a function conditionally or pass it as a callback
Arrow functionYou need a short callback, especially in array methods like map and filter

There is no single right answer. Many codebases use all three. The important thing is to understand how each one behaves so you can choose intentionally.

Common Mistake: Confusing Declaration with Call

A function reference without parentheses is not a call:

javascriptjavascript
function getTime() {
  return new Date().toLocaleTimeString();
}
 
console.log(getTime);   // [Function: getTime] -- the function object
console.log(getTime()); // "2:30:45 PM"               -- the return value

Accidentally omitting parentheses is one of the most common beginner bugs. If a function that should return a value instead prints something like [Function: ...], check for missing ().

Where to Go Next

Declaring a function writes the recipe, and calling it cooks the meal. Once declaring and calling feel natural, learn the difference between parameters and arguments to understand exactly what happens at call time.

Rune AI

Rune AI

Key Insights

  • Declare a function with the function keyword, a name, parameters, and a body in curly braces.
  • Call a function by writing its name followed by parentheses containing arguments.
  • Function declarations are hoisted. Function expressions and arrow functions are not.
  • A function call evaluates to the return value, or undefined if there is no return.
  • You can call the same function as many times as you need with different arguments.
RunePowered by Rune AI

Frequently Asked Questions

Can I call a function before declaring it?

Yes, but only if it is a function declaration. Function declarations are hoisted, meaning JavaScript moves them to the top of their scope before running the code. Function expressions and arrow functions are not hoisted.

What happens if I call a function with the wrong number of arguments?

JavaScript does not throw an error. Missing arguments become undefined inside the function. Extra arguments are ignored but still accessible through the arguments object or rest parameters.

Can I store a function call's result in a variable?

Yes. If the function returns a value, the result is assigned to the variable. If the function has no return statement, the variable gets undefined.

Conclusion

Declaring a function is writing the recipe. Calling it is cooking the meal. JavaScript gives you function declarations, function expressions, and arrow functions as three syntaxes for the same core idea. Understanding when each one is appropriate -- and how calling works with arguments, return values, and scope -- is the first practical skill every JavaScript developer needs.