Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

How to Check if an Array Is Empty or Does Not Exist in JavaScript?

Posted on January 16, 2025January 16, 2025 By Admin No Comments on How to Check if an Array Is Empty or Does Not Exist in JavaScript?

There are various methods to verify whether an array is empty or undefined in JavaScript.

1. Using if Statement

The simplest method to check if an array is empty or undefined is by using a basic if statement. You can test the array’s existence and its length.

let arr;

if (!arr || arr.length === 0) {
    console.log("Array is empty or does not exist");
} else {
    console.log("Array exists and is not empty");
}

2. Using Optional Chaining (?.)

Optional chaining (?.) allows you to check the existence of an array before accessing its properties, such as length.

let arr;

if (arr?.length === 0) {
    console.log("Array is empty");
} else if (!arr) {
    console.log("Array does not exist");
} else {
    console.log("Array exists and is not empty");
}

3. Combining Logical Operators

Logical operators like || and && can simplify checks for undefined and empty arrays.

let arr;

if (!(arr && arr.length)) {
    console.log("Array is empty or does not exist");
} else {
    console.log("Array exists and is not empty");
}

4. Using Array.isArray() Method

The Array.isArray() method explicitly checks if a variable is an array.

let arr;

if (!Array.isArray(arr) || arr.length === 0) {
    console.log("Array is empty or does not exist");
} else {
    console.log("Array exists and is not empty");
}

5. Using Ternary Operator

The ternary operator (? :) is a concise way to perform checks and execute actions based on the result.

let arr;

console.log(
    !arr ? "Array does not exist" :
    arr.length === 0 ? "Array is empty" :
    "Array exists and is not empty"
);

6. Using try…catch for Safety

In cases where you’re unsure if the variable is an array or if it might cause runtime errors, try...catch can safeguard your code.

let arr;

try {
    if (!arr || arr.length === 0) {
        console.log("Array is empty or does not exist");
    } else {
        console.log("Array exists and is not empty");
    }
} catch (error) {
    console.log("An error occurred:", error.message);
}
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Questions

Post navigation

Previous Post: Remove Object from Array using JavaScript
Next Post: How to Find If an Array Contains a Specific String in JavaScript/jQuery?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme