Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

Returning Multiple Values from a Function in JavaScript

Nikita Arora by Nikita Arora
January 29, 2024
Reading Time: 4 mins read
Return Multiple Values from a Function in JavaScript
Follow us on Google News   Subscribe to our newsletter

Functions usually return a single value, but there are situations where we might need to return multiple values. In these cases, it’s important to learn how to smoothly handle and get more than one value from a single function. In this article, we will learn how to return multiple values from a function in JavaScript.

How to Return Multiple Values From a Function?

Can we return multiple values from a function in JavaScript? Yes. we can return multiple variables simultaneously.

We will explore two main approaches: Using an Array and Using an Object. Let’s deep dive into each approach, providing examples and explanations along the way.

1) Using an Array

Let’s take an example of a function in which we want to return two values. We can pack the return values as elements of an array and return the array. 

For example, we have a function that returns firstName and lastName, we can return it in the form of a 2-sized array:

// Define a function called arrayMethod
function arrayMethod() {

  // Set the first name to 'Emma' and the last name to 'Watson'
  let firstName = 'Emma';
  let lastName = 'Watson';

  // Return an array containing the first and last names
  return [firstName, lastName];
}

Return values of the array can be accessed by using square bracket notation. Here ‘names’ is an array. 

// Call the arrayMethod function and store the result in the 'names' variable
let names = arrayMethod();

// Now, you can access the first name as names[0] and the last name as names[1]

console.log(names[0]); 
console.log(names[1])

Output:

Emma
Watson

Here values can also be extracted by array destructing.

// Call the getNames function and use array destructuring to get the returned values
let [firstName, lastName] = getNames();

By using the destructuring assignment syntax, we can unpack the values from the array and assign them to individual variables. In this case, the firstName variable will hold the value ‘Emma’, and the lastName variable will give the value ‘Watson’.

2) Using an Object

Arrays are fantastic, but sometimes we need more context. This approach provides a more readable and maintainable way of returning multiple values, as each value is labeled with a descriptive property name. It organizes the return values as key-value pairs within an object. 

Let’s explore this in more detail: 

// A function called objectMethod is defined
function objectMethod() {
  // Set the first name to 'Emma' and the last name to 'Watson'
  let firstName = 'Emma';
  let lastName = 'Watson';

  //An object containing the first and last name is returned
  return { firstName, lastName };
}

We can store the return values into an object and access them as object.firstName and object.lastName.

// Call the objectMethod function and store the result in the 'names' variable
let names = objectMethod();

// Now, you can access the first name as names.firstName and the last name as names.lastName
console.log(names.firstName);
console.log(names.lastName);

Output:

Emma
Watson

Comparing Both the Approaches

  • The object approach is more readable due to labeled properties, enhancing self-explanatory code while the Array approach may require additional documentation, therefore impacting readability.
  • The array approach is straightforward but less flexible if the order of the values needs to change or in the future new values are added whereas the Object method allows easy addition and removal of values.
  • Both are user-friendly approaches, choice depends on code needs. Array needs destructing and Object requires simple property access.

Also, learn how to convert an object to an array in JavaScript to know more about them.

Can a function have multiple return statements?

Yes, a JavaScript function can have multiple return statements. But note that when a return statement is encountered in a function, it immediately exits the function and returns the specified value.

Conclusion

In this article, we have covered two different methods to return multiple values from a function in JavaScript along with examples. By understanding these techniques, we can enhance the functionality and readability of our code.

ShareTweetShareSendSend
Nikita Arora

Nikita Arora

I'm Nikita, a B.Tech Computer Engineering student. My passion lies in web development, data structures, and algorithms. Committed to continuous learning, I find immense joy in sharing insights and fostering collaboration among like-minded peers in web development. Excited about exploring interesting opportunities in technology.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.