October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

JavaScript Functions: Define, Call, and Return Values Clearly

A clear beginner guide to JavaScript function definitions and calls, parameters and arguments, return values, declarations, expressions, arrow functions, and closures.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A JavaScript function is reusable behavior: define what it should do, call it to run that behavior, and use return to send a result back. Once you can distinguish a function’s definition from its call—and its parameters from the arguments you pass—the syntax becomes much easier to read.

What a function does

A function packages instructions so they can be run when needed. It can accept input, perform work, and produce an output. A function may also perform an action without returning a useful value, such as displaying a message.

As an Amazon Associate I earn from qualifying purchases.

Think of the definition as the reusable recipe and the call as asking JavaScript to follow it. Writing a definition alone does not run the function.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Define a function, then call it

This function takes a name and creates a greeting:

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

const message = greet("Mina");
console.log(message); // Hello, Mina!

function greet(name) { ... } defines the function. The line greet("Mina") calls it; the parentheses trigger execution. In the definition, name is a parameter—a named slot for input. In the call, "Mina" is an argument—the value supplied for that slot. Documentation and everyday conversation sometimes use “parameter” and “argument” less strictly, but this distinction is useful when learning.

Use return to get a result

A function call evaluates to the value produced by the last return statement it reaches. You can store that result, pass it to another function, or use it in an expression.

function add(a, b) {
  return a + b;
}

const total = add(2, 3);
console.log(total); // 5

Here, add(2, 3) evaluates to 5, which is assigned to total. If execution reaches the end of a function without reaching a return, the call evaluates to undefined.

function announce() {
  console.log("Ready!");
}

const result = announce();
console.log(result); // undefined

console.log displays a value; it does not return that value from announce. Displaying something and returning a result are separate jobs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a function form

Declarations, function expressions, and arrow functions all define functions, but they differ in syntax and behavior. There is no universally best form: choose according to when the function needs to be available and how it must behave.

Form Example When usable Return and behavior
Function declaration function greet() { ... } Hoisted within its applicable scope, so it can generally be called before its declaration. Scope details matter, including in strict-mode blocks and modules. Use an explicit return to return a value. Has its own function behavior, including its own this binding.
Function expression const greet = function () { ... }; Use after the variable has been initialized; the function expression is not hoisted as a callable definition in the same way as a declaration. Use an explicit return to return a value. A traditional function expression has its own this binding.
Arrow function const greet = () => ...; Use after the variable has been initialized, as with a function expression assigned to a variable. An expression body returns implicitly; a block body needs explicit return. It inherits this from its surrounding context and has no own arguments.

Function declarations

A declaration names the function directly:

sayHi(); // Hi

function sayHi() {
  console.log("Hi");
}

This works because declarations are hoisted within their applicable scope. Hoisting is a practical difference, not a reason to assume every declaration behaves identically in every scope.

Function expressions

A function expression creates a function that is commonly assigned to a variable:

const sayHi = function () {
  console.log("Hi");
};

sayHi(); // Hi

Call it after the variable has been initialized. Do not rely on calling this expression before its assignment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Arrow functions

Arrow functions provide a concise function-expression syntax. With an expression body, the expression’s value is returned automatically:

const square = (number) => number * number;

console.log(square(4)); // 16

If the body uses braces, it is a block body, so write return when you want to return a value:

const squareWithBlock = (number) => {
  return number * number;
};

const squareWithoutReturn = (number) => {
  number * number;
};

console.log(squareWithBlock(4)); // 16
console.log(squareWithoutReturn(4)); // undefined

The second arrow function calculates an expression but does not return it. Arrow functions also differ from traditional functions in contexts involving this, arguments, and constructor use; they are not simply interchangeable shorter syntax.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How scope leads to closures

A function can refer to variables in the scope where it was created. A closure is a function together with continued access to that surrounding lexical environment, even after the outer function has finished.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function makeGreeter(name) {
  return function () {
    return `Hello, ${name}`;
  };
}

const greetMina = makeGreeter("Mina");
console.log(greetMina()); // Hello, Mina

makeGreeter finishes, but the returned function still has access to the name value from its surrounding scope. Closures are a useful next topic once defining, calling, and returning from functions feels familiar.

A quick way to read function code

  • Find the definition: where is the reusable behavior written?
  • Find the call: where do parentheses run that behavior?
  • Match each argument in the call with its parameter in the definition.
  • Look for the executed return to see what value the call produces; if none is reached, the result is undefined.
  • For an arrow function, check whether its body is an expression or a braced block.

For reference, MDN’s JavaScript function guide covers definitions, calls, parameters, scope, closures, and arrow functions. Its function reference explains declaration and return behavior, and the arrow function reference details arrow syntax and its semantic differences.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.