

Javascript: Check if an array is empty – thispointer.com
source link: https://thispointer.com/javascript-check-if-an-array-is-empty/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

While working with arrays in javascript, often there is a requirement to check if an array is empty or not. This article will describe how to check if an array is empty or exists in javascript.
Table of Contents:-
Check if array is empty using isArray() and length property
Javascript’s isArray() method checks if the passed in argument value is an array or not.
Example:-
Check if the below arrays are empty or not
- [“Javascript”, “Is”, “Popular”,”Language”]
- [ ]
Function Code:-
function checkIfArrayEmpty(_array) { if (Array.isArray(_array) && _array.length) { console.log("Array Is Not Empty"); } else { console.log("Array Is Empty"); } }
Function Code:-
let array1 = ["Javascript", "Is", "Popular","Language"]; let array2 = [ ]; checkIfArrayEmpty(array1); checkIfArrayEmpty(array2);
Output:-
Array Is Not Empty Array Is Empty
Explanation:-
- Here, within the if statement, the first check is applied to find out if the passed in value is an array. Once the first check is passed, the code will perform the second check to see if the array’s length is greater than zero.
- If both the conditions are true, “Array Is Not Empty” is printed on the console, else “Array Is Empty” is printed.
Check if array is empty by checking if array is undefined and different operations on length
Example:-
Check if the below arrays are empty or not
- [“Javascript”, “Is”, “Popular”,”Language”]
- [ ]
Function Code:-
function checkIfArrayEmpty(_array) { if (typeof _array != "undefined" && _array != null && _array.length != null && _array.length > 0) { console.log("Array Is Not Empty") } else { console.log("Array Is Empty or Undefined") } }
Function Code:-
let array1 = ["Javascript", "Is", "Popular","Language"]; let array2 = [ ]; checkIfArrayEmpty(array1); checkIfArrayEmpty(array2);
Output:-
Array Is Not Empty Array Is Empty or Undefined
Explanation:-
- Here, within the if statement, four checks are applied:
- If the array is not undefined
- If the array is not null
- If the array’s length is not equal to null
- If the array’s length is greater than zero.
- All these checks verify if the array exists and if yes, then it’s not empty.
- If all the conditions are true, only then, “Array Is Not Empty” is printed on the console. Otherwise, if any of the conditions is false, “Array Is Empty or Undefined” is printed on the console.
Read More:
I hope this article helped you to check if an array is empty or not in javascript. Good Luck !!!
Recommend
-
9
We often use sorting while working with arrays. This article demonstrates easy ways to sort an array of strings by their length using different methods and various example illustrations. The strings within the array will be sorted based on th...
-
10
Very common requirement developers encounter while working with javascript arrays is to sort an array of integers. By default, an array containing digits will be considered as strings while sorting. This article demonstrates easy ways to sort...
-
8
While working in javascript, we sometimes encounter a requirement to check if an object is a string. This article demonstrates easy ways to check if a javascript object is a string using different methods and example...
-
12
This article will discuss how to convert Pandas Dataframe to Numpy Array. Table of Contents A Dataframe is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.Data...
-
8
This article will discuss how to convert Numpy arrays to a Pandas DataFrame. Table of Contents A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using panda...
-
5
Javascript: Check If an Object Empty While working in javascript, we often encounter a requirement to determine if the object is empty. This check is required at times before operating on the properties or met...
-
9
Javascript Check If an Object Is Null or Undefined While working in javascript, often we encounter a requirement that is to determine if the object is null or not. We might require this check to verify that th...
-
13
This tutorial will discuss about unique ways to check if numpy array contains only empty strings. Table Of Contents Method 1: using numpy.char.str_len() The numpy
-
7
This tutorial will discuss about unique ways to check if an array is empty or not in php. Table Of Contents Method 1: Using empty() function The empty() function in PHP ac...
-
8
Check if Array Contains Only Empty Strings in C++ This tutorial will discuss about a unique way to check if array contains only empty strings in C++. Suppose we have a string array. Like this,
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK