×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

How to check if an array is empty or exists using JavaScript?

JavaScript | Check if an array is empty or not: In this tutorial, we will learn how to check whether the given variable is an array, and non-empty using JavaScript? Learn with the help of an example. By Pratishtha Saxena, on July 21, 2023

An array is a collection of homogeneous data items stored at a contiguous memory location. All the data items are stored under a single name which is known as the name of the array. In order to check whether the declared variable is an array or not, the JavaScript method Array.isArray(varName) is used. Also, to check if the array is empty or not, the .length property of JavaScript is used.

Checking if an array is empty or exists using JavaScript

The steps to check if an array is empty or exists using JavaScript are:

  • Create an array or given an array, let it be score.
  • Check whether the given variable is defined or not using the typeof score != 'undefined' condition.
  • Check whether the given variable is an array type or not using the Array.isArray(score) condition.
  • Now, check the length of the array (using array.length property)if it is less than 0 then the given array is empty.
  • Put all these conditions with logical AND (&&) operation to check if an array is empty or exists. You can also use conditions separately to print the different messages based on the conditions.

Methods used to check if an array is empty or exists

The following are the two methods are which are using to check if an array is empty or exists using JavaScript -

  1. Array.isArray() Method
    This JavaScript method helps us to check whether the denoted variable is an array or not. It takes in the name of the variable, to be checked, as its parameter. This further returns the result in the form of true/false. If the variable is array it returns true otherwise false.
    Array.isArray(VarName);
  2. .length Property
    The length property of JavaScript is widely used when the length of a number, string, etc. is to be determined. It allows to get the numerical value of the element to be counted. For using it to find out whether the array is empty or not, it can be set to greater than 0 which implies that if there is even a single element present in the array, the array cannot be considered empty.
    score.length > 0;

JavaScript example to check if an array is empty or exists

The example given below shows that the declared variable is a non-empty array -

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  
  <body>
    <script type="text/javascript">
      // Create an array
      var score = [75, 89, 95, 64, 85];
      // Print the array 
      console.log(score);
      
      // Conditions to check if an array is empty or exists
      if (typeof score != 'undefined' && Array.isArray(score) && score.length > 0){
      console.log('Given variable is a non-empty ARRAY.');
      }
      else{
      console.log('Given variable is not a non-empty ARRAY.');
      }
    </script>
  </body>
</html>

The output of the above example is -

Check if an array is empty or exists

JavaScript Examples »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.