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 desk2 min

How to Get a Value Out of a JavaScript Function

Use return to pass a JavaScript function’s result to its caller. Learn how to capture it, avoid undefined, and return related values together.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use return to send a value from a JavaScript function to its caller, then use the function call where you need that value. For example, const result = getValue(); stores the returned result. Calling console.log() inside a function only displays a value; it does not return it.

Return a value, then use the function call

The return statement ends a function and specifies the value the caller receives. The function call expression evaluates to that value.

function getValue() {
  return 42;
}

const value = getValue();
console.log(value); // 42

You can store the result, pass it to another function, or use the call directly in a larger expression:

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

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

Returning a local variable works the same way as returning an expression. Use whichever form makes the calculation easiest to read:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function calculateArea(width, height) {
  const area = width * height;
  return area;
}

// Equivalent:
function calculateArea(width, height) {
  return width * height;
}

See MDN’s JavaScript return reference and guide to return values.

Why does a function return undefined?

An ordinary function returns undefined if it reaches its end without a value-bearing return. A bare return; also returns undefined, but exits the function immediately.

function showMessage() {
  console.log("Hello");
}

const result = showMessage(); // Logs "Hello"; result is undefined

Logging is not returning

console.log(result) writes a value for inspection; it does not make that value the result of the function call. If the caller needs the value, return it and then capture or use the call:

function getResult() {
  const result = 10 + 5;
  return result;
}

const answer = getResult(); // 15

Keep the expression with return

Do not put a line break between return and the expression. JavaScript’s automatic semicolon insertion treats the following example as a bare return, so it produces undefined:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function getValue() {
  return
    42;
}

Keep the expression on the same line, or open a parenthesis after return when the expression spans multiple lines:

function getValue() {
  return 42;
}

function getTotal() {
  return (
    20 +
    22
  );
}

MDN documents these behaviors in its return statement reference and function reference.

Return multiple related values together

A function call has one return value, but that value can be an object or an array. An object is often a clear way to return several named results:

function getRange(values) {
  return {
    min: Math.min(...values),
    max: Math.max(...values),
  };
}

const { min, max } = getRange([3, 8, 5]);
console.log(min, max); // 3 8

Arrays are useful when the values form an ordered group and their positions are clear to the caller. MDN’s functions reference covers JavaScript functions and returned values.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Returning a function is not the same as calling it

return someFunction; returns the function itself. return someFunction(); calls it and returns the value produced by that call.

function getValue() {
  return 42;
}

function getFunction() {
  return getValue; // Returns the function
}

function getComputedValue() {
  return getValue(); // Returns 42
}

Choose based on what the caller needs: a result, a deliberate side effect such as logging, or a function it can call later.

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
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

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.