Return multiple values in JavaScript?

前端 未结 20 1987
暖寄归人
暖寄归人 2020-11-22 13:17

I am trying to return two values in JavaScript. Is this possible?

var newCodes = function() {  
    var dCodes = fg.codecsCodes.rs;
    va         


        
相关标签:
20条回答
  • 2020-11-22 13:58

    All's correct. return logically processes from left to right and returns the last value.

    function foo(){
        return 1,2,3;
    }
    
    >> foo()
    >> 3
    
    0 讨论(0)
  • 2020-11-22 14:00

    Ecmascript 6 includes "destructuring assignments" (as kangax mentioned) so in all browsers (not just Firefox) you'll be able to capture an array of values without having to make a named array or object for the sole purpose of capturing them.

    //so to capture from this function
    function myfunction()
    {
     var n=0;var s=1;var w=2;var e=3;
     return [n,s,w,e];
    }
    
    //instead of having to make a named array or object like this
    var IexistJusttoCapture = new Array();
    IexistJusttoCapture = myfunction();
    north=IexistJusttoCapture[0];
    south=IexistJusttoCapture[1];
    west=IexistJusttoCapture[2];
    east=IexistJusttoCapture[3];
    
    //you'll be able to just do this
    [north, south, west, east] = myfunction(); 
    

    You can try it out in Firefox already!

    0 讨论(0)
  • 2020-11-22 14:01

    Few Days ago i had the similar requirement of getting multiple return values from a function that i created.

    From many return values , i needed it to return only specific value for a given condition and then other return value corresponding to other condition.


    Here is the Example of how i did that :

    Function:

    function myTodayDate(){
        var today = new Date();
        var day = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
        var month = ["January","February","March","April","May","June","July","August","September","October","November","December"];
        var myTodayObj = 
        {
            myDate : today.getDate(),
            myDay : day[today.getDay()],
            myMonth : month[today.getMonth()],
            year : today.getFullYear()
        }
        return myTodayObj;
    }
    

    Getting Required return value from object returned by function :

    var todayDate = myTodayDate().myDate;
    var todayDay = myTodayDate().myDay;
    var todayMonth = myTodayDate().myMonth;
    var todayYear = myTodayDate().year;
    

    The whole point of answering this question is to share this approach of getting Date in good format. Hope it helped you :)

    0 讨论(0)
  • 2020-11-22 14:01

    I am nothing adding new here but another alternate way.

     var newCodes = function() {
         var dCodes = fg.codecsCodes.rs;
         var dCodes2 = fg.codecsCodes2.rs;
         let [...val] = [dCodes,dCodes2];
         return [...val];
     };
    
    0 讨论(0)
  • 2020-11-22 14:03

    No, but you could return an array containing your values:

    function getValues() {
        return [getFirstValue(), getSecondValue()];
    }
    

    Then you can access them like so:

    var values = getValues();
    var first = values[0];
    var second = values[1];
    

    With the latest ECMAScript 6 syntax*, you can also destructure the return value more intuitively:

    const [first, second] = getValues();
    

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

    function getValues() {
        return {
            first: getFirstValue(),
            second: getSecondValue(),
        };
    }
    

    And to access them:

    var values = getValues();
    var first = values.first;
    var second = values.second;
    

    Or with ES6 syntax:

    const {first, second} = getValues();
    

    * See this table for browser compatibility. Basically, all modern browsers aside from IE support this syntax, but you can compile ES6 code down to IE-compatible JavaScript at build time with tools like Babel.

    0 讨论(0)
  • 2020-11-22 14:03

    It is possible to return a string with many values and variables using the template literals `${}`

    like:

    var newCodes = function() {  
        var dCodes = fg.codecsCodes.rs;
        var dCodes2 = fg.codecsCodes2.rs;
        return `${dCodes}, ${dCodes2}`;
    };
    

    It's short and simple.

    0 讨论(0)
提交回复
热议问题