-2

If you have a JavaScript function that returns two values, is it possible to assign those values to two variables in a single statement?

For example, in Python it might look like:

(var1, var2) = function()

I have tried a few things my tres look like this:

this.(var1, var2) = function();

I also tried something along the lines of:

this.var1, this.var2 = function();

and

this.var1 = this.var2 = function();

I also attempted using a single array instead of multiple variables, but it would complicate what I'm trying to accomplish later on because the two variables are used to story x and y coordinates.

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Chad
  • 29
  • 1
  • 5
  • Any ideas? You're supposed to try before asking. – Popnoodles Sep 21 '14 at 02:00
  • Have you tried writing a JavaScript function that returns two separate values? – ajp15243 Sep 21 '14 at 02:03
  • Does this help var1 = var2 = function() ? – andrex Sep 21 '14 at 02:03
  • JavaScript function can return an array or an object with several properties, but not two separate values. – PM 77-1 Sep 21 '14 at 02:03
  • Since your question assumes it is possible to write a function which returns two individual values, I'd be very interest to see an example... – JAAulde Sep 21 '14 at 02:04
  • @PM77-1 except that `[a, b] = somefunction()` works in Firefox like `list($a, $b) = somefunction()` in php. Allegedly. – Popnoodles Sep 21 '14 at 02:05
  • Whoever voted to close with "unclear what you're asking" - the question is very clear: *is it possible to assign those values to two variables in a single statement?* – Popnoodles Sep 21 '14 at 02:09
  • @Popnoodles It does work like that in Firefox, interestingly enough (just tried it). However, Chrome gives (more correctly, IMO) an error: `ReferenceError: Invalid left-hand side in assignment`. I wouldn't rely on such browser-dependent behavior, particularly when it's probably not correct according to spec. – ajp15243 Sep 21 '14 at 02:11
  • @ajp15243 me neither. The best answer would be no. – Popnoodles Sep 21 '14 at 02:11
  • @ajp15243: Destructuring assignment is a new feature coming in ECMAScript 6. – cookie monster Sep 21 '14 at 02:16
  • @cookiemonster Fancy, so Firefox is ahead of the game. – ajp15243 Sep 21 '14 at 02:17
  • @ajp15243: Mozilla's JavaScript is a superset of ECMAScript. They implement new features and then push for them to be adopted. Though sometimes the semantics change, and sometimes they just don't get added at all. They currently have other features too like Proxies, array comprehensions, and `let` variables. – cookie monster Sep 21 '14 at 02:19
  • @ajp15243 I don't think it's ahead of the game. It just tries to have something of its own. Internet Explorer also does the same thing. When it comes to **non-standard**, our code will become lengthier and I bet no one likes this. – King King Sep 21 '14 at 02:24
  • @KingKing: How will code become longer? – cookie monster Sep 21 '14 at 02:27
  • @KingKing cookie monster indicated as much, with them implementing new features and then pushing them to be standard. Every major browser does that, given the nature of browsers and web development. Edit: Yes, the code-becoming-longer statement confuses me a bit too. – ajp15243 Sep 21 '14 at 02:27
  • @cookiemonster you have to support **all** popular browsers, **not just FireFox**. – King King Sep 21 '14 at 02:28
  • @KingKing: Anyone supporting all browsers simply won't use that syntax because it'll throw an error. The ones using Mozilla-only features will more likely be extension developers. – cookie monster Sep 21 '14 at 02:29
  • @cookiemonster that's why the code becomes lengthier. If it's a standard you just need 1 code for all. – King King Sep 21 '14 at 02:30
  • @KingKing: It doesn't become any longer if you don't use it. It's not like someone is going to write their application twice just so that they can use destructuring assignment in the Mozilla browsers. – cookie monster Sep 21 '14 at 02:31
  • @cookiemonster I cannot understand why you don't get this point. You have a code FireFox understands but other browsers **can't**, so what you do? you have to support many the things like `if FireFox then .... else if Internet Explorer then ...` Anyway that's the most different thing (I hate best) between web programming and **desktop** programming. – King King Sep 21 '14 at 02:34
  • @cookiemonster I think King King means his/her point from the perspective of someone who is either dead-set on, or has their hands tied with, using the browser-specific code. (Correct me if I'm wrong) – ajp15243 Sep 21 '14 at 02:35
  • @KingKing: It's so incredibly simple. If you need to support browsers other than Firefox, then you don't use Firefox-only syntax extensions. Your example of `if FireFox then ... else ...` won't work because if you use Firefox syntax extensions, the entire program will fail to parse unless you use `try/catch`. – cookie monster Sep 21 '14 at 02:36
  • @cookiemonster That's the way I do. I've limitedly used something non-standard (even in CSS). So that's why such a private feature is useless unless for those who intend to support only FF. – King King Sep 21 '14 at 02:38
  • @KingKing: CSS is different. The browser flags add extra code. I think vendors are backing away from that practice, though it was useful for getting new features out. Kind of a pain to support. It's a pretty different situation with actual syntax extensions though. – cookie monster Sep 21 '14 at 02:40
  • @cookiemonster Aside: Even `try`/`catch` didn't work in Chrome for me: `function f(){return [1,2]}; try{if (false){ [a,b]=f() }else{ console.log('foo') }}catch{ console.log('fail') }`. Still threw `ReferenceError` I pasted in an above comment. – ajp15243 Sep 21 '14 at 02:42
  • @cookiemonster the same here is code becomes lengthier, of course there is the so-called prefix-free but looks like it cannot solve all the cases. – King King Sep 21 '14 at 02:44
  • @ajp15243: Ah yeah, a try/catch wouldn't work for syntax errors. Or wait, it's a ReferenceError like you said. Strange that the error isn't caught – cookie monster Sep 21 '14 at 04:32

3 Answers3

2

The answer is yes you can, but it only works in some browsers as an experimental technology, part of the Harmony (ECMAScript 6) proposal, so it's not recommended at the time of posting this answer.

See the Destructuring assignment docs

function foo(){
    return ["one", "two", "three"];
}

var [one, two, three] = foo();

You can do it in two lines which I appreciate isn't what you want but it works.

var foo = foo();
var one = foo[0], two = foo[1], three = foo[2];
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
1

Your question implies there is a way to do this, which is not true at least not at this time. As PM 77-1 said, a JavaScript function can return an array or an object with several properties, but not two separate values.

Here's an example of both:

var one, two;

// Return two values using an array
function foo(a, b) {
 return [a, b];
}

// Return two values using a plain object 
function bar(a, b) {
 return { one: a, two: b };
}

// Assign by index
var testOne = foo(1, 2);
one = testOne[0], two = testOne[1];

// Assign by properties
var testTwo = bar(1, 2);
one = testTwo.one, two = testTwo.two;

I would also like to add this is a duplicate question, please see: Return multiple values in JavaScript?

Community
  • 1
  • 1
  • Given that the linked question at the bottom of your answer is already itself a duplicate of another, I'd link [the original question](http://stackoverflow.com/q/2917175/1883647) instead. – ajp15243 Sep 21 '14 at 02:33
0

No, but you could return an array and then access them through the array

var returnvalues = function(){  

return [value1, value2];  
};
Russell Bevan
  • 334
  • 1
  • 13