Suppose we have a JS array or NodeList (for example using querySelectorAll).

We are not sure whether this array contains elements or not, and we would like to check just that.

Placing only the array variable within an if statement will always return true, even if the array does not contain elements.

Same thing about checking if the variable is not equal to null.

This is because the array itself exists (even if empty).

Instead, to check if an array (or querySelectorAll) contains elements, we can use the .length method: if (elements.length).

For example:

const inputs = document.querySelectorAll('input');

if (inputs) {
	// Always true - even if there are no inputs.
}

if (inputs !== null) {
	// Always true - even if there are no inputs.
}

if (inputs.length) {
	// True - only if there are inputs.
}