Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript Return multiple values from function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will see how to return multiple values from a function. In order to return multiple values from a function, we can not directly return them. But we can return them in form of an Array and Object. 

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

html




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>JavaScript | Return multiple
        values from function
    </h3>
  
    <p id="GFG_UP" style="color:green;
                          font-size: 20px;">
  
    </p>
  
    <button id="GFG_Button" onclick="returnVal()">
        ReturnValues
    </button>
  
    <p id="GFG_P" style="color:green; 
                         font-size: 20px;">
          
    </p>
  
    <script>
        var up = document.getElementById("GFG_UP");
        var down = document.getElementById("GFG_P");
          
        function set(){
              
            return ["GFG_1", "GFG_2"];
        }
          
        up.innerHTML = set;
          
        function returnVal() {
            down.innerHTML = set();
        }
    </script>
</body>

Output:

 

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

html




<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>JavaScript | Return multiple
        values from function
    </h3>
    <p id="GFG_UP" style="color:green; 
                          font-size: 20px;">
  
    </p>
  
    <button id="GFG_Button" onclick="returnVal()">
        ReturnValues
    </button>
  
    <p id="GFG_P" style="color:green; 
                         font-size: 20px;">
  
    </p>
  
    <script>
        var up = document.getElementById("GFG_UP");
        var down = document.getElementById("GFG_P");
          
        function set() {
              
            return {
                Prop_1: "Value_1",
                Prop_2: "Value_2"
            };
        }
          
        up.innerHTML = set;
          
        function returnVal() {
            var val = set();
            down.innerHTML = "Prop_1 = " + val['Prop_1']
                    + "<br>Prop_2 = " + val['Prop_2'];
        }
    </script>
</body>

Output:

 


My Personal Notes arrow_drop_up
Last Updated : 06 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials