Use $.isArray() to check whether an object is an array. Then you can check the truthness of the length property to see whether it is empty.

if( !$.isArray(siteArray) ||  !siteArray.length ) {
    //handler either not an array or empty array
}
Answer from Arun P Johny on Stack Overflow
🌐
CoreUI
coreui.io › blog › how-to-check-if-an-array-is-empty-in-javascript
How to check if an array is empty in JavaScript? · CoreUI
February 7, 2024 - A concise way to check for an empty ... becomes true. ... Using JSON.stringify: Convert the array to a JSON string and compare it with the string representation of an empty array....
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to check if a json object is empty or not in javascript
How to Check if a JSON Object is Empty or Not in JavaScript
March 28, 2024 - This method includes a check to see if the object is null or undefined, along with verifying if the object has no enumerable properties. const isEmpty = (obj) => { return obj === null || obj === undefined || Object.keys(obj).length === 0; }; const myObject = {}; console.log(isEmpty(myObject)); // Output: true · Incorporate these methods into your JavaScript projects to efficiently validate and handle empty JSON objects.
Top answer
1 of 16
706
if (typeof image_array !== 'undefined' && image_array.length > 0) {
    // the array is defined and has at least one element
}

Your problem may be happening due to a mix of implicit global variables and variable hoisting. Make sure you use var whenever declaring a variable:

<?php echo "var image_array = ".json_encode($images);?>
// add var  ^^^ here

And then make sure you never accidently redeclare that variable later:

else {
    ...
    image_array = []; // no var here
}
2 of 16
355

To check if an array is either empty or not

A modern way, ES5+:

if (Array.isArray(array) && array.length) {
    // array exists and is not empty
}

An old-school way:

typeof array != "undefined"
    && array != null
    && array.length != null
    && array.length > 0

A compact way:

if (typeof array != "undefined" && array != null && array.length != null && array.length > 0) {
    // array exists and is not empty
}

A CoffeeScript way:

if array?.length > 0

Why?

Case Undefined
Undefined variable is a variable that you haven't assigned anything to it yet.

let array = new Array();     // "array" !== "array"
typeof array == "undefined"; // => true

Case Null
Generally speaking, null is state of lacking a value. For example a variable is null when you missed or failed to retrieve some data.

array = searchData();  // can't find anything
array == null;         // => true

Case Not an Array
Javascript has a dynamic type system. This means we can't guarantee what type of object a variable holds. There is a chance that we're not talking to an instance of Array.

supposedToBeArray =  new SomeObject();
typeof supposedToBeArray.length;       // => "undefined"

array = new Array();
typeof array.length;                   // => "number"

Case Empty Array
Now since we tested all other possibilities, we're talking to an instance of Array. In order to make sure it's not empty, we ask about number of elements it's holding, and making sure it has more than zero elements.

firstArray = [];
firstArray.length > 0;  // => false

secondArray = [1,2,3];
secondArray.length > 0; // => true
🌐
UiPath Community
forum.uipath.com › help › studio
Check if an json array is empty - Studio - UiPath Community Forum
April 5, 2020 - Anyone knows how to check an json array is empty? I have gathered a few json arrays but some of them are empty, could anyone advise me how to verify if any json array is empty? Regards, Victor
Find elsewhere
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-check-if-object-is-empty
How to Check If an Object Is Empty in JavaScript | Built In
function isEmptyObject(obj){ return JSON.stringify(obj) === '{}' } ... These five quick and easy ways to check if an object is empty in JavaScript are all you need to get going.
🌐
Ash Allen Design
ashallendesign.co.uk › blog › how-to-check-if-an-array-is-empty-in-javascript
How to Check If an Array Is Empty in JavaScript
January 8, 2024 - So if the array is empty, the length property will return 0. We can then check against this integer to determine whether the array is empty or not. If the array is empty, the expression will return true like so: ... There are some caveats to ...
🌐
Sentry
sentry.io › sentry answers › javascript › how do i test for an empty javascript object?
How do I Test for an Empty JavaScript Object? | Sentry
This method was used as an alternative to using Object.keys before it was added to JavaScript in the 2011 ECMAScript 5 specification and is widely supported by browsers. You can use JSON.stringify() to convert the value to a JSON string to check if the value is an empty object.
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-an-array-is-empty-or-not-using-jquery
How to check an array is empty or not using jQuery?
Overview In jQuery, we can easily check whether an array is empty or not by using multiple methods. The collective way includes length property and as in JavaScript arrays are the object so using the jQuery alias symbol $.isEmptyObject(objName). An a
🌐
SitePoint
sitepoint.com › javascript
JSON array is empty - JavaScript - SitePoint Forums | Web Development & Design Community
November 25, 2010 - I tried using the code from http://www.factsandpeople.com/facts-mainmenu-5/26-html-and-javascript/89-jquery-ajax-json-and-php · but it seems like the content type passed string/array/object is not recognized properly at the server side.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
How to Check if an Object Is Empty in JavaScript | Envato Tuts+
April 29, 2022 - The JSON.stringify method is used to convert a JavaScript object to a JSON string. So we can use it to convert an object to a string, and we can compare the result with {} to check if the given object is empty.
🌐
freeCodeCamp
freecodecamp.org › news › check-if-javascript-array-is-empty-or-not-with-length
How to Check if a JavaScript Array is Empty or Not with .length
October 5, 2020 - To open up your console in Chrome, you can click Inpsect -> Console. First, create an array with no items in it. Next, let's use the logical "not" operator, along with our .length property, to test if the array is empty or not.
🌐
GoLinuxCloud
golinuxcloud.com › home › javascript › 6 methods to check if array is empty in javascript
6 Methods to check if Array is Empty in JavaScript | GoLinuxCloud
April 15, 2024 - Combining .length with Array.isArray(): First, check if it’s an array and then see whether it’s empty. Using every() Method: Ensure that all elements meet an empty condition (often impractical for mere emptiness check). Conditional check (Falsy Check): Utilize the feature of JavaScript treating zero as falsy.