To find the length of an array in TypeScript, read its length property: arr.length returns the number of elements as a number, and an empty array gives 0. There’s no size() or count() method on arrays:
const count = states.length; // 4
I ran every snippet with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the output you see is real.
In this tutorial, we’ll cover typescript array length basics, sparse arrays, readonly arrays and tuples, counting matches, and sizes of nested arrays, objects, Maps and Sets.
Get the array length with the length property
length is a property, so there are no parentheses. It updates as items change, and arr[arr.length - 1] grabs the last element:
const states: string[] = ["Texas", "Ohio", "Utah", "Iowa"];
console.log("states:", states.length);
const empty: number[] = [];
console.log("empty:", empty.length);
console.log("last item:", states[states.length - 1]);
console.log("at(-1): ", states.at(-1));
states.push("Maine");
console.log("after push:", states.length);
Output:
states: 4
empty: 0
last item: Iowa
at(-1): Iowa
after push: 5

push() bumps the length to 5.at(-1) is the modern shortcut. Adding items is covered in how to add elements to an array in TypeScript.
Why is my array length bigger than the number of items?
Arrays can have holes. length is one more than the highest index, so empty slots still count:
const slots = new Array<string>(3);
console.log("new Array(3):", slots.length, slots);
const seats: string[] = ["Ava", "Liam"];
seats[5] = "Noah";
console.log("seats.length:", seats.length);
console.log("real values: ", seats.filter(() => true).length);
const tasks = ["Email", "Invoice", "Call", "Report"];
tasks.length = 2;
console.log("truncated:", tasks);
tasks.length = 0;
console.log("cleared:", tasks, tasks.length);
Output:
new Array(3): 3 [ <3 empty items> ]
seats.length: 6
real values: 3
truncated: [ 'Email', 'Invoice' ]
cleared: [] 0

seats[5] makes the length 6 with only three real values, and setting length cuts or clears the array.filter(() => true) skips holes, so its length is the true count. A smaller length deletes items from the end, and length = 0 empties the array in place.
Length of readonly arrays and tuples in TypeScript
TypeScript adds two checks here. A readonly array’s length can’t be assigned, and a tuple’s length is a literal type:
const codes: readonly string[] = ["NY", "CA", "TX"];
codes.length = 0;
const point: [number, number] = [40.71, -74.01];
type PointLength = (typeof point)["length"];
const size: PointLength = 3;
Output:
len_readonly.ts(2,7): error TS2540: Cannot assign to 'length' because it is a read-only property.
len_readonly.ts(6,7): error TS2322: Type '3' is not assignable to type '2'.

length on a readonly array and knows the tuple’s length is exactly 2.Both errors appear at compile time, so you catch them before the code runs. More on locking arrays down is in readonly arrays in TypeScript.
Count the items in an array that match a condition
Often you want to know how many items pass a test. Chain filter() and read length, or count with reduce() if you don’t need the items:
interface Order {
id: number;
city: string;
total: number;
shipped: boolean;
}
const orders: Order[] = [
{ id: 1001, city: "Austin", total: 84.5, shipped: true },
{ id: 1002, city: "Denver", total: 129.99, shipped: false },
{ id: 1003, city: "Boston", total: 42, shipped: true },
{ id: 1004, city: "Austin", total: 310.25, shipped: true },
{ id: 1005, city: "Seattle", total: 18.75, shipped: false },
];
console.log("orders:", orders.length);
const shipped = orders.filter((o) => o.shipped).length;
console.log(`shipped: ${shipped} of ${orders.length}`);
const austin = orders.reduce((n, o) => (o.city === "Austin" ? n + 1 : n), 0);
console.log("Austin orders:", austin);
const big = orders.filter((o) => o.total >= 100);
console.log(`$100.00+ orders: ${big.length}`);
Output:
orders: 5
shipped: 3 of 5
Austin orders: 2
$100.00+ orders: 2

The filter method in TypeScript guide shows more conditions, and adding the totals is in how to sum an array in TypeScript.
Length of nested arrays, objects, Maps and Sets
length on a 2D array counts rows only. Objects have no length at all, and Maps and Sets use size instead:
const grid: number[][] = [[1, 2, 3], [4, 5], [6, 7, 8, 9]];
console.log("rows:", grid.length, "| cells:", grid.flat().length);
const menu: Record<string, number> = { coffee: 4.5, bagel: 3.25, juice: 5.99 };
console.log("object keys:", Object.keys(menu).length);
const zips = new Map([["Austin", "78701"], ["Denver", "80202"]]);
console.log("map size:", zips.size);
const tags = new Set(["sale", "new", "sale", "clearance"]);
console.log("set size:", tags.size);
Output:
rows: 3 | cells: 9
object keys: 3
map size: 2
set size: 3
flat() joins the rows so you can count every cell, and the Set drops the duplicate “sale”. If you’re checking text, see string length checks in TypeScript; for object counts, key-value pairs in TypeScript explains Record types.
Does checking length make arr[0] safe in TypeScript?
Not with the noUncheckedIndexedAccess option on. Every index read becomes T | undefined, and a length check doesn’t narrow it:
const names: string[] = ["Emma", "Olivia"];
if (names.length > 0) {
console.log(names[0].toUpperCase());
}
Output:
len_unchecked.ts(4,15): error TS2532: Object is possibly 'undefined'.
The compiler reports error TS2532, Object is possibly ‘undefined’. Read the element into a variable and check that instead; I compiled this fix with the same flag:
const first = names[0];
if (first !== undefined) console.log(first.toUpperCase());
For more ways to test for empty arrays, see how to check if an array is empty in TypeScript. You can also make non-empty arrays in TypeScript part of the type.
Keep going with these related array tutorials:
- Initialize an array in TypeScript
- Sort arrays in TypeScript
- Concatenate arrays in TypeScript with concat()
- Convert a Set to an array in TypeScript
Frequently asked questions
How do I get the length of an array in TypeScript?
Read arr.length. It’s a property, not a method, and returns a number.
Is there a size() or count() method for TypeScript arrays?
No. Arrays use length. Maps and Sets use size, and objects need Object.keys(obj).length.
Why is array length one more than the last index?
Indexes start at 0, so 4 items use indexes 0 to 3, and the last item is arr[arr.length - 1].
Can I clear an array by setting length to 0?
Yes, arr.length = 0 empties it in place. It fails to compile on a readonly array.
How do I count elements that meet a condition?
Use arr.filter(fn).length, or count with reduce().
Where is Array length documented?
See Array: length on MDN.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn