• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to Check if Array is Empty in jQuery/Javascript?

Aniruddha Chaudhari/471/0
CodeHTML & CSS

This is a very common question. And while working on a project, at least one time you will come across the situation where you have to check whether the given JavaScript array is empty or not.

I have seen many times, that developers use array-check to verify if the array is empty or not. This is not the correct way of doing this.

Let’s take an example.

let sample_arr = []

if (sample_arr) {
    console.log("Array is not empty.");
}
else {
    console.log("Array is empty.");
}

Output:

Array is not empty.

But this is not the expected output as an array sample_arr is an empty array. Beware and don’t do this.

This small mistake can lead to real damage and a nightmare situation for your project.

Instead of this, use the length property of the JavaScript array.

Check if array is empty in jQuery using the length property

Here is the JavaScript code.

let sample_arr = []

if (sample_arr.length>0) {
    console.log("Array is not empty.");
}
else {
    console.log("Array is empty.");
}

Output:

Array is empty.

Basically, the length property returns the number of elements or items in the array.

As this is simple JavaScript code (Vanilla JS), it will also work with jQuery.

You can also rewrite the above code with the simple inline if-else statement.

let sample_arr = []

sample_arr.length>0 ? console.log("Array is not empty.") : console.log("Array is empty.");

Output:

Array is empty.

Here if and else block is just a one-line code. In this case, it is standard coding practice to use inline if-else code.

Hope you find this simple and easy tutorial to check if array is empty in jQuery / JavaScript useful for your coding. Keep learning!

Python Interview Questions eBook

JavaScript
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

HTML CSS & JavaScript

  • HTML- First Program
  • HTML- Basic Tags
  • HTML- Adding CSS
  • HTML- Best Form Design Practices
  • HTML- Text Indent using CSS
  • HTML- Drop Cap using CSS
  • HTML- Add Favicon
  • HTML- Adding ToC on Website
  • HTML- Save Page as PDF
  • CSS- 17 Tips for Optimizing CSS
  • CSS- Adding Image Shadow
  • Show/Hide Password on Login Form
  • Pass Data to Another Web Page [JS]
  • Horizontal Scrolling on Mobile Web

HTML Editing Tools

  • HTML- Online Editor
  • HTML- Novi Builder

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard