How to Declare and Use Arrays in TypeScript

TL;DR
An array is a special variable that stores multiple values in a single variable, unlike a primitive variable that holds only one value at a time. Arrays are ordered, zero-indexed collections declared with square brackets or the Array keyword, and in TypeScript they are dynamic with no fixed size. Fixing the size turns an array into a tuple.
Transcript
All right. So today uh we'll see arrays concept. So in the last classes uh we have seen functions,  loops, conditional statements and some basics of programming languages. And uh today we'll  focus on arrays concept. So in every programming language uh we have same concept arrays concept.  Even Typescript, JavaScript also we have a same arra... Read More
Key Insights
- An array is a special type of variable that stores multiple values, which can be of the same data type or different data types, in a single variable.
- A primitive variable such as number, string, or boolean can hold only a single value at a time, and assigning a new value replaces the old one, which is why arrays are needed for groups of values.
- In TypeScript and JavaScript an array is directly a collection, whereas Java separates normal arrays from array lists, with the array list being the collection and the array being different.
- Arrays can be declared two ways: with square brackets or with the Array keyword, which is called the generic way and is a predefined derived data type in TypeScript and JavaScript.
- Array indexing always starts from zero, so an array of size five has indexes 0 to 4, meaning the last index is always n minus 1 where n is the array length.
- Storing a value at an index beyond the last position, such as the fifth location in a five-size array, throws an array index out of bound exception because index four is the upper bound.
- Arrays are an ordered collection, meaning values are retrieved in the same order they were added, and the index represents the address, location, or position of each element.
- In TypeScript and JavaScript arrays are dynamic with no fixed size, but specifying a fixed size makes it a tuple, which behaves like an array except its size is fixed.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: What is an array in TypeScript?
An array is a special type of variable that stores multiple values in a single variable. Unlike a primitive variable such as number, string, or boolean, which can hold only one value at a time, an array can hold a group of values. Those values can be of the same data type or of different data types. It is also called a collection in TypeScript and JavaScript because it groups elements together under one variable name.
Q: Why can't a primitive variable store multiple values?
A primitive variable, meaning a normal variable with a data type like number, string, or boolean, can hold only a single value at a time. If you try to store another value into it, the old value is replaced by the new one. This makes it impossible to store a group of values, such as all employee IDs, student IDs, or employee names, in one primitive variable, which is why the array concept is used to store multiple sets of data in a single variable.
Q: How do you declare an array in TypeScript?
There are two approaches to declare an array in TypeScript. The first uses square brackets, for example writing the let keyword, a variable name, a colon, the data type such as string, and square brackets, then assigning empty brackets. The second, called the generic way, uses the Array keyword, which is a predefined class and a derived data type in JavaScript and TypeScript. Writing empty brackets without assigning data is simply declaration, while adding values is called initialization.
Q: Why does array indexing start from zero?
Arrays follow the indexing concept where the index represents the position, location, or address of an element. Indexing always starts from zero, so the first element is at index zero. If an array has a size of five, the indexes run from 0 to 4, which is 0 to n minus 1. This means the starting index is zero (the lower bound) and the last index is always n minus 1 (the upper bound), so a five-size array's last index is four.
Q: What happens if you access an index beyond the array size?
If you try to store a value at a location beyond the last valid index, the program throws an array index out of bound exception. For example, in an array of size five, valid indexes range from zero to four, so there is no location for index five even though the size is five. Because indexing starts at zero, four is the last index, and attempting to store a value at the fifth location causes the out of bound exception.
Q: What does it mean that arrays are an ordered collection?
Arrays being an ordered collection means that values are stored and retrieved in the same order they were added. If you add elements in the order 1, 2, 3, 4, 5, they are stored in memory in that same order, and when you retrieve them you get them back as 1, 2, 3, 4, 5. This consistent ordering between insertion and retrieval is why an array is also called an ordered collection in TypeScript and JavaScript.
Q: What is the difference between an array and a tuple in TypeScript?
An array and a tuple are similar in that both store multiple values, but the major difference is size. In TypeScript and JavaScript an array is dynamic and has no fixed size, so you can store n number of values in it. A tuple, on the other hand, is fixed in size. When you specify a fixed size for what would otherwise be a dynamic array, it becomes a tuple, meaning the number of values it can hold is fixed rather than flexible.
Q: How do arrays differ between TypeScript and Java?
In TypeScript and JavaScript, an array is directly a collection and a single array type supports all the properties that would otherwise require a separate structure. In Java, there are two different things: a normal array and an array list. Both behave similarly, but the array list is called the collection while the normal array is separate. In TypeScript and JavaScript, this distinction disappears because one array type handles both roles and is itself considered a collection.
Summary & Key Takeaways
-
The session covers the arrays concept in TypeScript, building on earlier lessons about functions, loops, and conditional statements. An array is defined as a variable that can hold multiple values of the same or different data types, solving the limitation that primitive variables like number, string, and boolean store only one value at a time.
-
Arrays are declared using square brackets or the Array keyword (the generic way), and they follow zero-based indexing where the index represents the position or address of each element. For an array of size five, indexes run from 0 to 4, so the last index equals n minus 1, and accessing beyond it throws an array index out of bound exception.
-
Arrays are an ordered collection, retrieving elements in the same order they were stored. In TypeScript and JavaScript arrays are dynamic with no fixed size, unlike Java which separates arrays and array lists. Declaring an array with empty brackets is declaration, and adding values is initialization; fixing an array's size creates a tuple.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from SDET- QA 📚






Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator