JavaScript return

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

return exits a function immediately and optionally sends a value to the caller. With no expression, the function returns undefined.

JavaScript Syntax

JavaScript
function add(a, b) {
  return a + b;
}
console.log(add(2, 3));
Try it Editor

How It Works

  1. Compute. a + b is 5.
  2. return sends 5. Caller receives it.

Examples

Example 1 — Early return

Guard clauses keep code flat.

JavaScript
function label(n) {
  if (n < 0) return "neg";
  return "ok";
}
console.log(label(-1));
console.log(label(2));
Try it Editor

How It Works

  1. Negative path returns immediately.
  2. Otherwise returns ok.

Example 2 — Bare return

No expression → undefined.

JavaScript
function f() { return; }
console.log(f());
Try it Editor

How It Works

  1. return; exits with undefined.

Example 3 — Arrow shorthand

Expression-body arrows return implicitly.

JavaScript
const double = (n) => n * 2;
console.log(double(4));
Try it Editor

How It Works

  1. No braces means automatic return.
  2. See Arrow Functions concept.

Frequently Asked Questions

Return an array or object, then destructure at the call site.
No. return is only valid inside functions (and some module/eval contexts).

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful