JavaScript Emptiness
This
[].isEmpty() // true
is easier to read than
[].length === 0 // true
But if you try that you will get an error saying
Uncaught TypeError: [].isEmpty is not a function
You can easily prototype this behavior.
Arrays
Array.prototype.isEmpty = function() {
return this.length === 0;
}
// Examples
[].isEmpty(); // true
const a = [];
a.isEmpty(); // true
Strings
String.prototype.isEmpty = function() {
return this.length === 0;
}
// Examples
"".isEmpty(); // true
const s = '';
s.isEmpty(); // true
Objects
{}.isEmpty();
wont work but it does if you define an object first.
Object.prototype.isEmpty = function() {
return Object.keys(this).length === 0;
}
Will work if you first define an object.
// Examples
const o = {};
o.isEmpty(); // true
Conclusion
Let's sum this up with a little "file" you can use
// isEmpty.js
Array.prototype.isEmpty = function() {
return this.length === 0;
}
String.prototype.isEmpty = function() {
return this.length === 0;
}
Object.prototype.isEmpty = function() {
return Object.keys(this).length === 0;
}