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 #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

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 #community:Codecademy-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?

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