• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Check if Array is Empty

Using JavaScript to Check if Array is Empty

September 20, 2022 Leave a Comment

We can easily use JavaScript to check if an array is empty. An empty array has length 0, and is equal to False, so to check if an array is empty, we can just check one of these conditions.

var emptyArray = [];

#length check
if ( emptyArray.length == 0){
  console.log("Array is empty!");
}

#if statement check
if (!emptyArray.length) {
  console.log("Array is empty!");
}

In JavaScript, arrays are a collection of objects which are ordered. When working with arrays, it can be useful to be able to easily determine if the array is empty.

There are a few ways you can determine if an array is empty.

The length of an empty array will be 0. So we can use the length property to check for this. The length of an empty array will be equal to 0, or false, so we can also use this to determine if an array is empty.

In the following JavaScript code, you can see the two ways we can check if an array is empty or not.

var emptyArray = [];

#length check
if ( emptyArray.length == 0){
  console.log("Array is empty!");
}

#if statement check
if (!emptyArray.length) {
  console.log("Array is empty!");
}

Checking if an Array is Empty with an if Statement in JavaScript

One fact we can use in JavaScript to check if an array is empty is that an array that is empty is equivalent to the boolean value False.

In this case, we can test if an array is empty using a simple if statement.

var emptyArray = [];

#if statement check
if (!emptyArray.length) {
  console.log("Array is empty!");
}

Checking if an Array is Empty Using the JavaScript Length Property

One of the ways we can easily check if an array is empty in JavaScript is with the JavaScript length property.

The length of an array which is empty is 0.

Checking to see if an array is empty using the JavaScript length property is shown in the following JavaScript code.

var emptyArray = [];

if ( emptyArray.length == 0){
  console.log("Array is empty!");
}

Hopefully this article has been useful for you to learn how to use JavaScript to check if an array is empty.

Other Articles You'll Also Like:

  • 1.  JavaScript acos – Find Arccosine and Inverse Cosine of Number
  • 2.  JavaScript Trig – How to Use Trigonometric Functions in Javascript
  • 3.  Using JavaScript to Remove the First Character From a String
  • 4.  innerHTML vs outerHTML
  • 5.  Using Javascript to Remove the Id from a Div
  • 6.  Using JavaScript to Get the Min Value in an Array
  • 7.  Using JavaScript Math Module to Get Euler’s Constant e
  • 8.  How to Generate a Random Letter In JavaScript
  • 9.  Check if a String Contains Uppercase Letters in JavaScript
  • 10.  Using JavaScript to Reverse a Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy