6

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.

5

6 Answers 6

10

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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, or, you could include TypeScript definition files (.d.ts) and use TypeScript's Tuple type.
Doesn't this though leave out the main use case for a tuple i.e being fix length and also immutable?
@Minsky you're absolutely right. Currently, there are several options available to get close to the tuple concept in JavaScript, some of which include using Object.freeze() on array (which makes an array immutable), or using TypeScript.
7

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

Comments

2

There are no tuples in javascript. It returns the last evaluated value when a tuple is expressed in javascript.

const value = (1, 2, 3);
// value is three

I have no idea why it exists though.

Comments

0

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

Comments

0

JavaScript does not have native tuples, but you could possibly roll your own. For example:

class Tuple extends Array {

    constructor(...args) {
        super();
        this.push(...args);
        Object.seal(this);
    }
    
}

The tuple in this case is essentially just an array, but because it's sealed you cannot add or remove elements from it, so it will act like a tuple.

You can then create tuples like this:

let t = new Tuple(1,2)

2 Comments

This only prevents adding or removing elements from an array. However, one feature of Python's tuple is that it can compare two tuples directly. For example, (1, 'a') == (1, 'a') evaluates to True. But not with this solution.
I have only provided a very basic implementation as an example. You are free to add a method for comparison of tuples.
-4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.