{"id":12772,"date":"2025-07-15T07:06:06","date_gmt":"2025-07-15T07:06:06","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12772"},"modified":"2025-07-15T07:06:06","modified_gmt":"2025-07-15T07:06:06","slug":"check-if-array-is-empty-or-does-not-exist","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/javascript\/check-if-array-is-empty-or-does-not-exist","title":{"rendered":"How to Check If an Array is Empty or Does Not Exist?"},"content":{"rendered":"<p>When working with arrays in JavaScript, it&#8217;s common to need a quick way to check if the array contains any elements. This is especially useful when processing data fetched from an API, form inputs, or user selections.<\/p>\n<h3>What is an Array in JavaScript?<\/h3>\n<p>An <strong>array <\/strong>in JavaScript is a special type of object used to store multiple values in a single variable. Each value (or element) has a numeric index.<\/p>\n<p><code>const fruits = [\"Apple\", \"Banana\", \"Cherry\"];<\/code><\/p>\n<h3>How to Check if an Array is Empty?<\/h3>\n<p>To check if a JavaScript array is empty, you simply check its .length property. The .length property returns the number of elements in the array.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\narray.length === 0\r\n<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst items = [];\r\nif (items.length === 0) {\r\n console.log(\"The array is empty.\");\r\n} else {\r\n console.log(\"The array is NOT empty.\");\r\n}\r\n<\/pre>\n<p><strong>Output: <\/strong><br \/>\n<code>The array is empty.<\/code><\/p>\n<h3>Creating a Reusable Function<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nfunction isArrayEmpty(arr) {\r\n return Array.isArray(arr) && arr.length === 0;\r\n}\r\n\/\/ Usage\r\nconsole.log(isArrayEmpty([]));          \/\/ true\r\nconsole.log(isArrayEmpty([1, 2, 3]));   \/\/ false\r\nconsole.log(isArrayEmpty(\"Not array\")); \/\/ false\r\n<\/pre>\n<p>Using Array.isArray() ensures the input is actually an array, avoiding errors if the input is something else like null or undefined.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with arrays in JavaScript, it&#8217;s common to need a quick way to check if the array contains any elements. This is especially useful when processing data fetched from an API, form inputs, or user selections. What is an Array in JavaScript? An array in JavaScript is a special type of object used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12773,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-12772","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12772"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12772"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12772\/revisions"}],"predecessor-version":[{"id":12774,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12772\/revisions\/12774"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12773"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}