JavaScript function return multiple values | Use Array Example

Can function return multiple values in JavaScript?

No, the JS function could not return multiple value returns but it could return an array containing multiple values or as properties of an object.

Note: JavaScript functions can return only single value.

JavaScript function return multiple values Example code

Let’s see HTML example code for How to Return multiple values in JavaScript.

Return as Array

Get the values

Complete example code:

Output:

JavaScript function return multiple values

Return as object

Output:

JavaScript function return multiple values Example

Object with labels

If you want to put “labels” on each of the returned values (easier to maintain), you can return an object:

Output:

labeled object Return multiple values in JavaScript

Do comment if you any doubts and suggestions on this JS function topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

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