In TypeScript, you can also use the length
property of the array to check if it is not empty.
In this example,
numbers
with some elements.notEmpty
.let numbers: number[] = [1, 2, 3];
let notEmpty: boolean = numbers.length !== 0;
console.log(`Array is not empty: ${notEmpty}`);
Array is not empty: true
In this example,
emptyArray
.notEmpty
.let emptyArray: number[] = [];
let notEmpty: boolean = emptyArray.length !== 0;
console.log(`Array is not empty: ${notEmpty}`);
Array is not empty: false
In this tutorial, we learned How to check if an Array is Not Empty in TypeScript language with well detailed examples.