Navigate back to the homepage

How to check array is empty in typescript?

Infinitbility
TypeScript
November 7th, 2021 · 1 min read
How to check array is empty in typescript?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to check an array is empty or not in typescript, here we will use array length to check array has value or not but before checking the array, we have to check the array is defined or not.

Let start today tutorial How to check an array is empty in typescript?

for check array is defined, we are able to use typeof operator or for check empty array we have to just use array.length syntax.

First, let’s create a new array with no elements.

1const arr: Array<Object> = [];

Now we can check if the array is empty by using .length.

1arr.length

This will return 0, as there are 0 items in the array.

1const users: Array<Object> = [];
2
3if(typeof users !== "undefined" && users.length == 0){
4 console.log("Array is empty");
5}

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to check array contains value in typescript?

How to check array contains value in typescript?

Example of check array contains value or not in typescript

November 6th, 2021 · 1 min read
How to check if index exists in array javascript?

How to check if index exists in array javascript?

Example of check index exist in array in javascript, React, React Native, vue, node, deno, and all javascript framework.

November 5th, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]