javascript check if array is not empty
// To safely test if the array referenced by variable “array” isn’t empty:
if (array && array.length) {
// not empty
} else {
// empty
}
// Note that if “array” is assigned to some other object that isn’t an array, the code will still execute the “else” part.
// A possible improvement to check that the object is specifically an empty array might be this:
if (array && array.constructor === Array && array.length === 0) {
// strictly an empty array
} else {
// either null, or not an array or not an empty array
} if (array && !array.length) {
// array is defined but has no element
}if (array === undefined || array.length == 0) {
// array empty or does not exist
}
if (Array.isArray(array) && array.length) {
// array exists and is not empty
}array.length = []empty array check javascript
Also in JavaScript:
- react setState
- javascript date now
- Can't bind to 'formGroup' since it isn't a known property of 'form'
- javascript change select options dynamically
- how to import js via script in react
- how to check if 2 images are touching js
- document ready without jquery
- get serialize data javascript
- change color of strike through line of text in react native
- match ids from 2 arrays in javascript asynchronous programming
- how to cut off decimals in javascript
- json decode android
- array object inside an arrar in vue
- javascript update text in div
- npm can i use my modules without specifying the path
- how to add data to array in javascript dynamically
- react client real ip address
- js capitalize word
- jquery is checked
- java script functions
- express unique validator for the owner
- c# newtonsoft serialize dictionary to json
- how to get selected list item value in javascript
- pdfjs customizing viewer.html js event handler