Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. full-stack-python-architect-mastering-flask-react-ai-first-development
  4. /
  5. How to Return Values From JavaScript Functions (With Examples)

FULL STACK PYTHON ARCHITECT: FLASK+ REACT + VIBE CODING
Web Development
0 views

How to Return Values From JavaScript Functions (With Examples)

Learn how the JavaScript return statement actually works, why console.log() isn't a substitute for returning values, and how to avoid the most common mistakes that leave beginners staring at 'undefined'. Packed with practical examples you can use right away.

A
Author
July 17, 2026

Introduction: Why the Return Statement Matters

If you've spent any time writing JavaScript, you've probably typed console.log() a hundred times without thinking twice. But here's a question that trips up even intermediate developers: what happens to the value your function calculates if you never return it?

The answer is simple but important: it disappears. Once a function finishes running, any value that wasn't explicitly returned is gone, and your variable ends up with undefined instead of the result you expected. Understanding the JavaScript return statement is one of the most fundamental skills you need to write functions that actually produce usable output — not just functions that print something to the console and then vanish into thin air.

In this tutorial, we'll break down exactly how return values in JavaScript work, walk through practical examples, and highlight the mistakes that cause so much confusion for beginners.

What Does the Return Keyword Actually Do?

The return keyword does two things inside a JavaScript function:

  • It stops the execution of the function immediately.
  • It sends a value back to wherever the function was called.

That returned value can then be stored in a variable, passed to another function, used in a calculation, or displayed however you like. This is very different from simply logging something to the console, which only displays information — it doesn't give you anything to work with afterward.

A Basic Example

Let's look at a simple function that adds two numbers:

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

const result = addNumbers(5, 10);
console.log(result); // 15

Notice what's happening here. The function addNumbers doesn't print anything itself. Instead, it returns the sum of a and b, and we store that returned value inside the variable result. Only afterward do we log it to the console. This separation — calculating a value versus displaying a value — is exactly the concept covered in the course's lecture on returning values from functions.

Return vs. console.log(): A Critical Difference

One of the most common misconceptions among new JavaScript developers is thinking that console.log() and return do the same job. They don't.

  • console.log() simply prints a value to the browser or terminal console for you to see. It has no effect on your program's logic.
  • return sends a value back out of the function so it can be reused, stored, passed along, or processed further in your code.

Consider this flawed example:

function multiplyNumbers(a, b) {
  console.log(a * b);
}

const product = multiplyNumbers(4, 5); // logs 20
console.log(product); // undefined

Even though 20 gets printed to the console, the variable product is undefined. That's because the function never used the return keyword — it only displayed the value without actually handing it back to the calling code. This is one of the most frequent bugs new developers run into, and it perfectly illustrates why understanding JavaScript functions and return behavior is so essential.

What Happens When a Function Doesn't Return Anything?

By default, if a function doesn't explicitly use return, JavaScript automatically returns undefined. This isn't an error — it's expected behavior — but it can cause confusing bugs if you're not expecting it.

function sayHello(name) {
  console.log(`Hello, ${name}!`);
}

const greeting = sayHello('Sam');
console.log(greeting); // undefined

Here, sayHello only logs a greeting to the console. Since there's no return statement, the variable greeting receives undefined. If your goal is to actually use the greeting message elsewhere in your code (say, inserting it into a webpage), you'd need to return the string instead of just logging it:

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

const greeting = sayHello('Sam');
console.log(greeting); // "Hello, Sam!"

Storing and Reusing Returned Values

The real power of the return keyword comes from being able to store a function's output and use it later, potentially multiple times, in different parts of your program.

function calculateTax(price) {
  return price * 0.08;
}

const tax1 = calculateTax(100);
const tax2 = calculateTax(250);

console.log(`Tax on $100: $${tax1}`);
console.log(`Tax on $250: $${tax2}`);

Here, calculateTax is a small, reusable function. Because it returns a value instead of just logging it, we can call it as many times as we want with different arguments and store each result separately. This is the foundation of writing reusable, testable JavaScript functions — a skill that becomes even more important once you start working with parameters, arguments, and more complex logic, all of which are explored step-by-step in the full course.

Returning Values from Arrow Functions

Arrow functions follow the same return rules, but they offer a shorthand syntax when the function body is a single expression:

// Regular function
function square(num) {
  return num * num;
}

// Arrow function (implicit return)
const squareArrow = (num) => num * num;

Notice that the arrow function version doesn't need the return keyword or curly braces at all — when the function body is a single expression, JavaScript automatically returns that value. This is called an implicit return, and it's a great example of how modern JavaScript syntax can make your code more concise once you understand the underlying return behavior.

Common Mistakes When Returning Values

Even experienced developers occasionally slip up with return statements. Here are a few mistakes to watch out for:

  • Forgetting the return keyword entirely — leading to undefined where you expect real data.
  • Using console.log() instead of return — which displays a value but doesn't make it usable elsewhere.
  • Returning inside a loop incorrectly — remember that return immediately exits the function, so any code after it (including remaining loop iterations) won't execute.
  • Expecting multiple return statements to run — only the first return that gets executed will fire; the function stops there.
function checkNumber(num) {
  if (num > 0) {
    return 'Positive';
  }
  return 'Non-positive';
  console.log('This will never run'); // unreachable code
}

In the example above, the console.log() after the second return statement will never execute, because the function has already exited by that point.

Why This Matters for Real-World JavaScript

Return values aren't just an academic concept — they're the backbone of virtually every practical JavaScript application. Whether you're processing form data, iterating over arrays with methods like forEach, working with object literals, or building asynchronous functions that fetch data from an API, you'll constantly rely on functions that return meaningful values instead of just printing them to the console.

This concept ties directly into other core JavaScript building blocks like function parameters and arguments, the difference between methods and functions, and even asynchronous programming with setTimeout and network calls. Mastering how return values work early on will make all of these more advanced topics click much faster.

Featured Video

[Video Embed Placeholder: JavaScript — Return Values From Functions]

Watch the full lecture walkthrough where these concepts are demonstrated step-by-step with live coding examples, showing exactly how the return keyword works differently from console.log() and how to avoid the undefined trap in your own projects.

Key Takeaways

  • The return statement sends a value out of a function so it can be stored and reused — it doesn't just display it.
  • console.log() is for viewing output; return is for producing usable results.
  • Functions without an explicit return automatically return undefined.
  • Once a return statement executes, the function stops immediately — no code after it will run.
  • Arrow functions support implicit returns for single-expression function bodies.

These fundamentals form the building blocks for everything else you'll do in JavaScript, from working with arrays and objects to handling asynchronous operations. If you'd like a deeper, hands-on walkthrough of these concepts — along with parameters, arguments, arrow functions, and real-world async examples — the Full Stack Python Architect: Flask + React + Vibe Coding course covers all of this and more, guiding you from JavaScript fundamentals all the way to building full production-ready applications.

Tags:
javascript return statement
return values javascript
javascript functions return
return keyword javascript
Share Article

Back to All Articles

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.