I would love to know if there are python type tuples in JavaScript. I am working on a project and I need to just use a list of objects rather tan an array.
-
4No, it doesn't. You can use an array of fixed size as a generic tuple or an object with the same properties as "named" tuples. – VLAZ Feb 07 '20 at 08:26
-
2TypeScript maybe? https://www.typescriptlang.org/docs/handbook/basic-types.html#tuple – wentjun Feb 07 '20 at 08:29
-
3Does this answer your question? [JavaScript variable assignments from tuples](https://stackoverflow.com/questions/4512405/javascript-variable-assignments-from-tuples) – VLAZ Feb 07 '20 at 08:30
-
so yes and no, because any array or object can work as tuple, but because of the loosly typed paradigm, you have to take care of your self. – Nina Scholz Feb 07 '20 at 08:52
-
You can use destructuring feature for tuple like behaviour https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – Md. Khirul Islam Omi Feb 07 '20 at 08:53
4 Answers
Javascript does not support a tuple data type, but arrays can be used like tuples, with the help of array destructuring. With it, the array can be used to return multiple values from a function. Do
function fun()
{
var x, y, z;
# Some code
return [x, y, z];
}
The function can be consumed as
[x, y, z] = fun();
But, it must be kept in mind that the returned value is order-dependent. So, if any value has to be ignored, then it must be destructured with empty variables as
[, , x, , y] = fun();

- 1,102
- 1
- 12
- 18
-
There's also a couple of ways you can declare what types this function returns at each position in the array, which many modern text editors will parse, understand, and show to devs who import your function on mouseover etc: you could define a JS tuple's shape in a [JSDoc comment using `@typedef`](https://stackoverflow.com/questions/64739315/how-to-document-tuple-using-jsdoc), or, you could include [TypeScript definition files (`.d.ts`)](https://stackoverflow.com/questions/61932377) and use [TypeScript's Tuple type](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types). – user56reinstatemonica8 Jul 25 '22 at 08:50
-
The closest to a tuple is using Object.seal()
on an array which is initiated with the wanted length:
let arr = new Array(1, 0, 0);
let tuple Object.seal(arr)
Otherwise, you can use a Proxy
:
let arr = [ 1, 0, 0 ];
let tuple = new Proxy(tuple, {
set(obj, prop, value) {
if (prop > 2) {
throw new RangeError('this tuple has a fixed length of three');
}
}
});
Update: There is an ECMAScript proposal for a native implementation of a Tuple
type

- 5,457
- 3
- 26
- 42
No Javascript doesn't have tuples so my advise will be to use an array of fixed size as a default tuple or maybe as an object which has the same properties as "named" tuples. Tuples are only created using arrays

- 350
- 2
- 13
Sometimes it's just better to answer question directly without prolonging it with but you can but it can,
This should be more helpful for someone like me.
Does javascript have tuples type built in like ones in python?
Answer: No, tuples are not native types in javascript. Period.

- 307
- 1
- 3
- 11