Alex Rivera | Logout

Is it possible in JavaScript to simultaneously assign to multiple variables like it is in Python?

Asked 2010-12-22T18:31:28.513
145

In some languages, like Python, you can define and assign values to a tuple of variables, and retrieve their values like this:

name, age = ("Bob", 24)
print(name)           # name evaluates to "Bob"
print(age)            # age evaluates to 24

Is there anything similar in JavaScript? Or do I just have to do it the ugly way with an array:

tuple = ["Bob", 24];
name = tuple[0];      // name Evaluates to Bob
age = tuple[1];       // age Evaluates to 24

Is there a better way to simulate Python tuples in JavaScript?

Edit
Report

1 Answer

5

Unfortunately you can't use that tuple assignment syntax in (ECMA|Java)Script.

EDIT: Someone linked to Mozilla/JS 1.7 - this wouldn't work cross-browser but if that is not required then there's your answer.

answered 2010-12-22T18:32:32.880

Your Answer