I am trying to return two values in JavaScript. Is this possible?
var newCodes = function() {
var dCodes = fg.codecsCodes.rs;
var dCodes2 = fg.codecsCodes2.rs;
return dCodes, dCodes2;
};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, but you could return an array containing your values:
Then you can access them like so:
This is called destructuring assignment and is supported by every major JS environment. It’s equivalent to the following:
You can also return an object if you want to assign a name to each value:
And to access them:
Which is the same as:
It is highly recommended to return an object instead of an array unless the values make sense as a simple tuple, e.g. a coordinate pair
[x, y]. With an array, it’s easy to forget which value is which, it’s harder to add more values later, and it’s marginally more difficult to correctly type with TypeScript or JSDoc.