Ad space (banner)
🟨JavaScript Lessons
Lesson 48 / 69

Returning Multiple Values from a Function (Array Destructuring)

In this lesson you'll learn how to return multiple values from a function using an array, so you can receive several results together from a single function. This is for people searching "JavaScript how to return multiple values from a function."

If a function returns an array and the caller destructures it, you can write code that looks as though "multiple values are being returned at the same time." Picture it as: "put two cards, name and score, into one box and hand it over." A JavaScript function can technically only return one value, but wrapping it in an array or object lets you effectively hand back several results bundled together to the caller.

The sample code has the minMax function return an array, [minimum, maximum], and the caller receives it with destructuring, like const [min, max] = minMax(...). If you return an object instead of an array, you can attach names, as in { min, max }, resulting in code whose intent is even clearer.

A common beginner stumbling block is the order of the variables receiving the destructured values. When returning an array, the receiving side must line up variables in the exact same order, and getting the order wrong assigns unintended values. If it's getting hard to tell what each value means, switching to a design that returns an object instead of an array is one option.

Designing carefully how a return value should be received lets you build easy-to-use functions. This style is commonly used in real projects for processing where a set of values naturally goes together, like coordinates (x, y) or a range (minimum, maximum).

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)