Return multiple values in JavaScript?

前端 未结 20 1974
暖寄归人
暖寄归人 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 14:21

    A very common way to return multiple values in javascript is using an object literals, so something like:

    const myFunction = () => {
      const firstName = "Alireza", 
            familyName = "Dezfoolian",
            age = 35;
      return { firstName, familyName, age};
    }
    

    and get the values like this:

    myFunction().firstName; //Alireza
    myFunction().familyName; //Dezfoolian
    myFunction().age; //age
    

    or even a shorter way:

    const {firstName, familyName, age} = myFunction();
    

    and get them individually like:

    firstName; //Alireza
    familyName; //Dezfoolian
    age; //35
    

提交回复
热议问题