Multiple Return Values

Many modern languages allow you to easily return multiple distinct values from a single function. For example, Python allows this natively:

# Python Example
def get_user():
    return "name@domain.com", 21, "active"

email, age, status = get_user()

The JavaScript Limitation

In JavaScript, that syntax is not allowed. A function can only return exactly one value.

If you attempt to separate return values with commas, JavaScript does not throw an error. Instead, it exhibits strange behavior: it evaluates all the expressions separated by commas but only returns the very last one.

function getUser() {
  // DON'T DO THIS
  return "name@domain.com", 21, "active";
}

const result = getUser();
console.log(result); // Output: "active" (The rest are discarded!)

The Solution

To get around this limitation and return multiple pieces of data simultaneously, JavaScript developers bundle the values into a single data structure, typically an Object or an Array.

function getUser() {
  // Return a single object containing multiple properties
  return {
    email: "name@domain.com",
    age: 21,
    status: "active"
  };
}

We will explore objects in more depth in later chapters.