Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Object values() Method
Next article icon

JavaScript Return multiple values from function

Last Updated : 04 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object. This approach allows functions to efficiently produce and deliver diverse sets of values, enhancing flexibility and versatility in JavaScript programming.

Below are the approaches used in JavaScript to Return multiple values from a function:

Table of Content

  • Using Array
  • Using an object

Approach 1: Using Array

The approach of using an array to return multiple values from a JavaScript function involves organizing the values into an array and then utilizing array destructuring to extract and assign those values to individual variables.

Example: This example returns the array ["GFG_1", "GFG_2"] containing multiple values. 

JavaScript
function set() {     return ["GFG_1", "GFG_2"]; }  let [x,y] = set();  console.log(x); console.log(y); 

Output
GFG_1 GFG_2 

Approach 2: Using an object

The approach of using an object to return multiple values from a JavaScript function involves organizing the values into key-value pairs within an object.

Example: This example returns the object return {Prop_1: "Value_1", Prop_2: "Value_2" }; containing multiple values. 

JavaScript
function set() {      return {         Prop_1: "Value_1",         Prop_2: "Value_2"     }; }  console.log(set);  function returnVal() {     let val = set();     console.log("Prop_1 = " + val['Prop_1']         + "Prop_2 = " + val['Prop_2']); } returnVal() 

Output
[Function: set] Prop_1 = Value_1Prop_2 = Value_2 

Next Article
JavaScript Object values() Method

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-functions
  • JavaScript-Questions

Similar Reads

  • How a Function Returns an Object in JavaScript ?
    JavaScript Functions are versatile constructs that can return various values, including objects. Returning objects from functions is a common practice, especially when you want to encapsulate data and behavior into a single entity. In this article, we will see how a function returns an object in Jav
    3 min read
  • JavaScript Generator return() Method
    JavaScript Generator.prototype.return() method is an inbuilt method in JavaScript that is used to return the given value and finishes the generator. Syntax: gen.return( value ); Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter holds the
    2 min read
  • JavaScript Object values() Method
    JavaScript object.values() method is used to return an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by the object manually if a loop is applied to the properties. Object.values() takes the object as an argument
    3 min read
  • JavaScript AsyncGenerator.prototype.return() Method
    The JavaScript built-in method AsyncGenerator.prototype.return() is used in returning a value from an asynchronous generator. This return() method can be called at any time during the iteration of an async generator, it will cease iterating and give back to you, as the user, a specific value when ca
    2 min read
  • JavaScript Function Call
    The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). This allows borrowing methods from other objects, executing them within a different context, overriding the default value, and passing arguments. Syntax: cal
    2 min read
  • How to call a function that return another function in JavaScript ?
    The task is to call a function that returns another function with the help of JavaScript is called a Currying function, a function with numerous arguments that can be converted into a series of nesting functions with the help of the currying method. Approach:Define Outer Function:Create an outer fun
    2 min read
  • JavaScript Return Statement
    The return statement in JavaScript is used to end the execution of a function and return a value to the caller. It is used to control function behaviour and optimise code execution. Syntax return [expression]Expression Evaluation: The expression inside the brackets is evaluated and returned to the c
    4 min read
  • JavaScript typedArray.forEach() Method
    The typedArray.forEach() is an inbuilt function in JavaScript which executes a given function once for each given typedArray element. Syntax: typedarray.forEach(callback) Parameter: It accepts a parameter "callback" function which produces each element of the new typedArray and this function takes t
    1 min read
  • JavaScript Map forEach() Method
    JavaScript Map.forEach method is used to loop over the map with the given function and executes the given function over each key-value pair. Syntax:myMap.forEach(callback, value, key, thisArg)Parameters:This method accepts four parameters as mentioned above and described below: callback: This is the
    3 min read
  • JavaScript Comma Operator
    JavaScript Comma Operator mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand. [GFGTABS] JavaScript let x = (1, 2, 3); console.log(x); [/GFGTABS]Output3 Here is another example to show that all expressions are actually executed. [GFGTABS] Java
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences