EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript tuple
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript tuple

TypeScript tuple

Introduction to TypeScript tuple

Whenever there is a need to store a collection of values belonging to various data types, which is not possible using arrays, then we make use of a data type called tuple in TypeScript, which can also be passed as parameters to functions and each value in a tuple is individually called item, and each item in a tuple is based on an index that is they can be accessed using their respective numeric index and the first index in a tuple starts from zero and it can go up to n-1 where n is the number of items in the tuple.

The syntax to declare tuple  in TypeScript is as follows:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

var name_of_the_tuple = [value1, value2…, valuen];

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

where name_of_the_tuple is the tuple name and

value1, value2, valuen are the values of different data types stored in a tuple.

Working of the tuple in TypeScript

  • A collection of values belonging to the same data types can be stored in an array, whereas a collection of values belonging to different data types cannot be stored in an array.
  • The data type used to store the collection of values belonging to different data types is called tuple in TypeScript.
  • The tuples can be passed as parameters to the functions in TypeScript.
  • Each value stored in a tuple is called an item, and each item is based on an index that is each item can be accessed using their respective numeric index.
  • The indexes for the items stored in a tuple begin from zero and can extend up to n-1, where n is the number of items stored in the tuple.
  • Items can be added to a tuple by making use of a function called push function.
  • Items can be removed from the tuple by making use of a function called pop function. The items are removed from the end of the tuple.
  • A tuple is mutable, which means changes to the tuple is allowed.

Examples of TypeScript tuple

Here are the following examples mention below

Example #1

TypeScript program to create a tuple and demonstrate the push operation to add the items to the tuple and pop operation to remove the items from the tuple and update the tuple to make changes to the existing items in the tuple and display the items of the tuple after each operation:

Code:

//creating a tuple called firstuple to store a collection of values of different data types
var firsttuple = [1, 'Welcome', 2, 'to', 3, 'EDUCBA'] //displaying the items present in the tuple
console.log('The items present in the tuple are:');
console.log(firsttuple[0]);
console.log(firsttuple[1]);
console.log(firsttuple[2]);
console.log(firsttuple[3]);
console.log(firsttuple[4]);
console.log(firsttuple[5]);
console.log('\n')
//adding items to the tuple and displaying the items of updated tuple as the output on the screen
firsttuple.push('learning');
console.log('The items present in the tuple after push operation are:');
console.log(firsttuple[0]);
console.log(firsttuple[1]);
console.log(firsttuple[2]);
console.log(firsttuple[3]);
console.log(firsttuple[4]);
console.log(firsttuple[5]);
console.log(firsttuple[6]);
console.log('\n')
//removing items from the tuple and displaying the items of the updated tuple as the output on the screen
firsttuple.pop();
firsttuple.pop();
console.log('The items present in the tuple after pop operation are:');
console.log(firsttuple[0]);
console.log(firsttuple[1]);
console.log(firsttuple[2]);
console.log(firsttuple[3]);
console.log(firsttuple[4]);
console.log('\n')
//updating the existing items in the tuple and displaying the items of the updated tuple as the output on the screen
firsttuple[0] = 'Hello';
firsttuple[2] = '';
firsttuple[4] = 'EDUCBA';
console.log('The items present in the tuple after update operation are:');
console.log(firsttuple[0]);
console.log(firsttuple[1]);
console.log(firsttuple[2]);
console.log(firsttuple[3]);
console.log(firsttuple[4]);
console.log('\n')

Output:

TypeScript tuple output 1

TypeScript tuple output 1.2

In the above program, we are creating a tuple called firsttuple to store a collection of values of different data types. Then we are displaying the items present in the tuple as the output on the screen. Then we are adding items to the tuple using push operation and displaying the items present in the updated tuple as the output on the screen. Then we remove the items from the tuple by using pop operation and displaying the items present in the updated tuple as the output on the screen. Then we are updating the existing items in the tuple and displaying the items present in the updated tuple as the output on the screen.

Example #2

TypeScript program to create a tuple and demonstrate the push operation to add the items to the tuple and pop operation to remove the items from the tuple and update the tuple to make changes to the existing items in the tuple and display the items of the tuple after each operation:

Code:

//creating a tuple called firstuple to store a collection of values of different data types
var firsttuple = [1, 'learning', 2, 'is', 3, 'fun'] //displaying the items present in the tuple
console.log('The items present in the tuple are:');
console.log(firsttuple[0]);
console.log(firsttuple[1]);
console.log(firsttuple[2]);
console.log(firsttuple[3]);
console.log(firsttuple[4]);
console.log(firsttuple[5]);
console.log('\n')
//adding items to the tuple and displaying the items of updated tuple as the output on the screen
firsttuple.push('EDUCBA');
console.log('The items present in the tuple after push operation are:');
console.log(firsttuple[0]);
console.log(firsttuple[1]);
console.log(firsttuple[2]);
console.log(firsttuple[3]);
console.log(firsttuple[4]);
console.log(firsttuple[5]);
console.log(firsttuple[6]);
console.log('\n')
//removing items from the tuple and displaying the items of the updated tuple as the output on the screen
firsttuple.pop();
firsttuple.pop();
console.log('The items present in the tuple after pop operation are:');
console.log(firsttuple[0]);
console.log(firsttuple[1]);
console.log(firsttuple[2]);
console.log(firsttuple[3]);
console.log(firsttuple[4]);
console.log('\n')
//updating the existing items in the tuple and displaying the items of the updated tuple as the output on the screen
firsttuple[0] = 'Hello';
firsttuple[2] = '';
firsttuple[4] = 'fun';
console.log('The items present in the tuple after update operation are:');
console.log(firsttuple[0]);
console.log(firsttuple[1]);
console.log(firsttuple[2]);
console.log(firsttuple[3]);
console.log(firsttuple[4]);
console.log('\n')

Output:

output 2

output 2.2

In the above program, we are creating a tuple called firsttuple to store a collection of values of different data types. Then we are displaying the items present in the tuple as the output on the screen. Then we are adding items to the tuple using push operation and displaying the items present in the updated tuple as the output on the screen. Then we remove the items from the tuple by using pop operation and displaying the items present in the updated tuple as the output on the screen. Then we are updating the existing items in the tuple and displaying the items present in the updated tuple as the output on the screen.

Recommended Articles

This is a guide to the TypeScript tuple. Here we discuss the concept of the tuple in TypeScript through definition, syntax, and implementation of tuple through programming examples and their outputs. You may also have a look at the following articles to learn more –

  1. TypeScript Operators
  2. TypeScript Types
  3. TypeScript Array
  4. Typescript Interview Questions
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more