FAQ: Arrays - Tuples

This community-built FAQ covers the "Tuples " exercise from the lesson “Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn TypeScript

FAQs on the exercise _Tuples _

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

The exercise tells me that tuples have fixed lengths. I wanted to see what happened if I tried to add values to it, I wasn’t able to add values by doing array[6] = ‘x’, but I was still able to do array.push(‘x’). When I printed to the console the length, it showed an increased length (now length 7 instead of 6), I’m also able to print the entire tuple, which includes my newly pushed value. I’m still unable to access the element by doing array[6], but when I do array.forEach(element, index), I’m able to print the value at array[6]. I’m curious as to why this is happening, any insight?

3 Likes

That action would have produced an array, not mutated the tuple.

Maybe I’m missing something, but I’m still confused. If it created an array, why can I not access that element directly? It’s still giving me the “no element” error.

Still pretty hazy with it comes to TS. Will need to go back and reread the unit that describes TS tuple objects. Sorry I couldn’t be more help. My bad.

That’s alright, I appreciate it, no need to apologize! :slight_smile:

1 Like

Found this page, just to correct my misunderstanding…

Summary: Points to remember

  • A tuple is a data container that can store multiple values of different types.
  • We use to initialize a tuple with values separated by a comma.
  • We do not add a type when initializing a tuple.
  • To access individual elements in a tuple, we refer to its position in the tuple index.
  • Tuples are mutable and element values can be changed by assigning new values to them.
  • We append new elements to a tuple with the .push() method.
  • We remove elements with .pop() , .shift() and .splice() .
  • Tuples can be destructured.
    TypeScript Tuples Tutorial | KoderHQ

Now to dig out the lesson where they are introduced.

1 Like

Specifically, we can’t assign an array to a tuple variable, even when the elements are of the correct type

Is it meant to say “we can’t assign a typed array to a tuple variable” or am I mistaken because in the used example, it looks like tup is being assigned an array, namely ['there', 'there']

let tup: [string, string] = ['hi', 'bye'];
let arr: string[] = ['there','there'];
tup = ['there', 'there']; // No Errors.
tup = arr; // Type Error! An array cannot be assigned to a tuple.

Sometime later in 2021 I found out that in Javascript you can create arbitrary properties onto an object (which includes arrays), and then assign values to those properties. kind of like a dictionary (Python terminology), or hashmap (Java terminology?). Although that was Javascript, and this tutorial was for TypeScript, there might be some similarities there. Just posting this as an “in case you were curious”. Started using codecademy again after a long hiatus, and saw a notification on this thread from the above post by micro, heh.

1 Like

It’s been three years since you asked the question, but I think I can explain why this is happening. It will be useful to others.

When we run the TS compiler, it finds an error in the line:

console.log(favoriteCoordinates[6]);

Indeed, there is no element under index 6. For now…

This is because the command favoriteCoordinates.push(-6.825); has not been executed yet.
Thus, the compiler does not take into account the code that will be executed in the future and looks at the fact.

But we can use a trick that will not displease the compiler and get access to the last added element [6].

let favoriteCoordinates: [number, number, string, number, number, string] = [17, 45, 'N', 142, 30, 'E'];

favoriteCoordinates.push(-6.825);

console.log(favoriteCoordinates[favoriteCoordinates.length - 1]);

favoriteCoordinates[favoriteCoordinates.length - 1] this is the same as favoriteCoordinates[6].
It works perfectly.
:grinning: