Multiple-Value Returns in JavaScript

Album Cover: Diamond Hoo Ha

"I got to get you in my suitcase."
Supergrass / Diamond Hoo Ha Man

Posted on September 04, 2006 3:29 PM in Programming
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Jonathan Snook posted today about the Firefox 2 Beta being available, and among the few things he listed as being new in the release were multiple-value returns in JavaScript.

One of the things I've always liked about Perl was having the ability to do things like the following:

sub getLatLon
 my ($address) = @_;
 my $lat = getLatitude($address);
 my $lon = getLongitude($address);
 return ($lat, $lon);

With a function like that, you could call my ($lat, $lon) = getLatLon(); at any time to get a coordinate pair. Since the logical pairing makes sense, there's no need for two separate functions and you end up writing less code.

The implementation in JavaScript would be pretty similar, but here it is just for the sake of completion:

function getLatLon(address)
 var lat = getLatitude(address);
 var lon = getLongitude(address);
 return [lat, lon];

Now I don't do nearly enough JavaScript coding to really merit me getting too excited about this new addition to the language (at least as it is supported by Firefox), but it's nice to know I'll have the option if the need ever presents itself in the future.


Sean on September 04, 2006 at 4:25 PM:

Well now that does sound interesting. That's also something I like about Python. It's so obvious, that I don't know why most programming languages don't implement it.


Ian Clifton on September 06, 2006 at 8:53 AM:

That's something I wondered about when I was first teaching myself programming, especially anything dealing with coordinates. You could create two functions to do virtually the same thing, or one function that has to do one or the other and be called twice.

In PHP you can sort-of accomplish somewhat the same thing using classes and setting $this->variable, but that's not truly returning multiple values, just assigning multiple variables within a class' function.

How do you assign the return values in Perl? Do you use comma separated variables or does it have to be an array?


Ian Clifton on September 06, 2006 at 8:54 AM:

Oops, just realized you did give an example of assigning the return values. My infamiliarity with Perl caused me to miss that.


Sean on September 07, 2006 at 10:23 PM:

You can kind of do this in PHP like this:

function myfunc() {
return array($x_pos, $y_pos);

list($x, $y) = myfunc();



kae verens on September 11, 2006 at 3:06 AM:

"return [lat, lon]" returns one variable, which is an array. This is not new.


Ricky on June 25, 2008 at 2:18 PM:

In FF you can do:

var v1, v2;
[v1, v2] = getLatLon(address);

But it doesn't work in IE.


Post Comments

If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.


Email Address:



Check this box if you hate spam.