I have the following jQuery code:
var shown = $('div.slideshow').find('div.slide:visible');
var next = shown.next();
if(next == '') {
console.log('empty');
}
Basically when the next comes back as empty like: []
I want to be able to detect this. How do I do it?
keys() is a static method that returns an Array when we pass an object to it, which contains the property names (keys) belonging to that object. We can check whether the length of this array is 0 or higher - denoting whether any keys are present or not. If no keys are present, the object is empty: Object.
return Object.keys(obj).length === 0 ;entries . This is typically the easiest way to determine if an object is empty.
Having confirmed that the variable is an array, now we can check the length of the array using the Array. length property. If the length of the object is 0, then the array is considered to be empty and the function will return TRUE. Else the array is not empty and the function will return False.
Use length
property, which contains number of elements within jQuery object:
if(next.length === 0) {
console.log('empty');
}
or:
if(!next.length) {
console.log('empty');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With