To check if an array is empty in TypeScript, you can use the length
property.
In this example,
numbers
with some elements.length
property with 0 and store the result in the variable isEmpty
.const numbers: number[] = [1, 2, 3];
const isEmpty: boolean = numbers.length === 0;
console.log(`Array is empty: ${isEmpty}`);
Array is empty: false
In this example,
emptyArray
.length
property with 0 and store the result in the variable isEmpty
.const emptyArray: number[] = [];
const isEmpty: boolean = emptyArray.length === 0;
console.log(`Array is empty: ${isEmpty}`);
Array is empty: true
In this tutorial, we learned How to check if an Array is Empty in TypeScript language with well detailed examples.