Computers

Learn to check if Javascript Array is Empty or not with length

JavaScript Array is Empty
If you’re programming in JavaScript, you are required to understand how to check if an array is empty.

You can utilize the .length property to see if the array is empty.

The length property returns or sets the number of elements in the array. If you know the number of elements in the array, you can see and know if it is empty. The empty array comprises 0 elements.

Let’s look at some examples to check if Javascript Array is Empty or not with length.

.length syntax example

 Const myArray = [`Horses`,` Dogs`, `Cats`];

Here, we are developing a variable that points to an array.

You can check the length of the array utilizing the length property.

 myArray.length

This returns 3 because the array contains 3 elements.

There are three methods to see if the .length array is empty. ..

.Length example 1

First, there is the requirement to do the creation of a new array with no elements.

const arr = []

Now you should be using .length to check if the array is empty. Address length

arr.length

This comes back to 0 because the array comprises 0 elements.

.Length example 2

You can also explicitly check if the array is empty.

if (arr.length === 0) {console.log ("The array is empty!")}

If the array is empty, the above message will be logged. If the array contains elements, the if block code will not be executed.

The third way to use .length to check if an array is empty is as under:-

.Length example 3

You can see if the “!” The symbol array is empty by making use of both the length property of JavaScript with the use of the logical “not” operator. The operator denies the expression.

That is, it can be used to return true if the array is empty. In this instance, we will open a JavaScript console. To open the console in Chrome, click Insects> Console.  Initially, you need to develop an array with no elements.

Javascript Array is Empty or with length

Then use the logical “not” operator and the .length property to test if the array is empty.

How to Check if a JavaScript Array is Empty or Not with .length

arr.length returned 0 if the “not”  operator was not used. Adding an operator returns true if the operand is false. It comes back to being true because arr.length is 0 or false.

Let’s use this in an if statement and dump the message when the array is empty.

JavaScript Array is Empty or Not with .length

When checking if an array is empty, it is often best to also check that the array is an array.

Why?

Because  you may want to check the length of a array, but instead get other data types such as strings: The length property can be used for other data types as well, so check if: It is recommended. The array is actually what you expect.

length property

It’s a good idea to also use the Array.isArray () method to make sure the array is an array. This method determines if the passed array is an array. If an array is passed, this method returns true.

Let’s add this method as an example.

Summary of how to use Array.isArray () method

how to use Array.isArray () method

In this article, you learned that there are several ways to check if an array is empty by using the length property in JavaScript. The length property returns the number of elements in the array.

You also learned that it’s best to use the Array.isArray method when using the .length property to verify that the passed values ​​are an array as expected. You can always come back to this article in case you face any issues while you are working and that work involves the use of Javascript.

About the author

Jeremmy Wade

Hey I am Jaremmy Wade a Tech blogger, writer and contributor i am working as blogger from last 11 years and co- founder of Etechnoblogs.com, also i contribute my thoughts about tech related or internet errors, computer or email errors and business , health tips writer also shares gaming platform knowledge some etc. IF you wish to write for us on E Techno Blogs then feel free to contact Me @ etechnoblogs@gmail.com

Add Comment

Click here to post a comment