I’ve been doing a lot of JavaScript lately and in my JS I’ve been testing if a string is empty, null or undefined. C# has string.IsNullOrEmtpy and I believe JS should have the same. I was about to write my on function, but decided to first see if someone else has done this. I found an example, but didn’t completely like it. So I made some changes and here’s String.IsNullOrEmpty
String.IsNullOrEmpty = function(value) {
var isNullOrEmpty = true;
if (value) {
if (typeof (value) == ‘string’) {
if (value.length > 0)
isNullOrEmpty = false;
}
}
return isNullOrEmpty;
}
var isNullOrEmpty = true;
if (value) {
if (typeof (value) == ‘string’) {
if (value.length > 0)
isNullOrEmpty = false;
}
}
return isNullOrEmpty;
}
I don’t know why it’s "String.IsNullOrEmpty" instead of "String.prototype.IsNullOrEmpty", but it works. Maybe someone can answer this for me.
Source