Skip to content

Return multiple values

Important note: Check browser compatibility for destructuring assignment. If you need to support older browsers, see the legacy example.

ECMAScript 6/2015 Way

The returning of multiple values is basically just the returning of an object:

 function getCar() {
    var assemblyYear = 1938;
    return { brand: "Volkswagen", line: "Beetle", year: assemblyYear, color: "white" }
}

Legacy Way

The legacy way just uses an array instead of an object:

function getCar() {
    var brand = "Volkswagen";
    var line = "Beetle";
    var assemblyYear = 1938;
    var color: "white";
    return [ brand, line, assembyYear, color ];
}

(source)

(source)

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.