Site icon Ampersand Tutorials

Arrays in TypeScript: Create, Print and Loop (2026)

Quick answer: Creating an array in TypeScript means holding several values of the same type in one variable, using square brackets and a type such as string

[] or number[]. You can fill it on one line or start it empty and push items later. Indexing starts at zero, and a loop prints every value.

Arrays: the box that holds your data

A single variable holds one value. An array holds a whole collection – and TypeScript makes sure every item in that collection is the same type. When you are managing lists of users, students or products, arrays are the foundation everything else is built on.

If variables are still new to you, go through our first program in TypeScript tutorial first – it teaches the variable basics you will build on here.

Creating and printing array in TypeScript

Creating an array of objects

Creating an array works just like creating a variable – you declare a name, then open a [ and list your values. Here we build an array of employee records, each with a name, an ID and a department:

let enq_List = [
    { name:"John", id:123, dept:"IT" },
    { name:"Victor", id:124, dept:"Finance" },
    { name:"Doe", id:125, dept:"HR" }
];

Because every record has the same shape, TypeScript can infer the type of the whole array and warn you the moment a record goes off-script – say, a record missing dept or using a number where a name should be.

Printing the whole array

To see everything the array holds, pass the array itself to console.log:

console.log(enq_List);

Then follow the familiar compile-and-run steps we used in the first program:

tsc demo.ts
node demo.js

Every record is printed in order:

{name:"John", id:123, dept:"IT"}
{name:"Victor", id:124, dept:"Finance"}
{name:"Doe", id:125, dept:"HR"}

Why print them one by one?

Logging the whole array is a great first step, but real programs rarely want the entire dump. Usually you want to show a list in the browser, sum the numbers, or pick out just one field. That is where iteration takes over – our guide to using a for loop to print an array shows exactly how to walk through every item (or just the names) with a few lines of code.

Keep building

Arrays hold data; functions and interfaces shape how you work with it. Level up with functions in TypeScript and interfaces – or step back and follow the entire route in our TypeScript for Beginners: The Complete Learning Path.

Official documentation

For the full reference behind this guide, read the TypeScript handbook on everyday types, including arrays. It is the source the details above are drawn from, and the first place to check when an option or attribute name changes.

Frequently asked questions

How do I create an array in TypeScript?

u003cpu003eUse square brackets and add the element type, like number array, then place your values inside the brackets separated by commas.u003c/pu003e

What is the difference between an empty and a missing array type?

u003cpu003eAn empty array type allows any value, while string array or number array keeps every item as the same type you choose.u003c/pu003e

How do I print every value in an array?

u003cpu003eUse a for loop over the array length, or a for…of loop, and log each item inside the loop.u003c/pu003e

Exit mobile version