42
\$\begingroup\$

I am aware of it being frowned upon to do something like write C# in JavaScript. (see this if you don't know what I'm talking about)

But as a judgement call, I think we could stand to have a relatively simple check for values that are null or empty, so I'm looking for feedback on this implementation of String.isNullOrEmpty.

String.isNullOrEmpty = function (value) {
    return (!value || value == undefined || value == "" || value.length == 0);
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Aren't the first checks redundant after you've already tried to call toString on value? \$\endgroup\$ – user786653 Oct 25 '11 at 19:50
  • \$\begingroup\$ Possibly.... I was thinking if I were to call a "static" String.isNullOrEmpty(). Not sure if I need it. \$\endgroup\$ – Terrance Oct 25 '11 at 20:04
  • \$\begingroup\$ That revision is no good, see this and other posts. You've radically changed the meaning of the function. \$\endgroup\$ – user786653 Oct 25 '11 at 20:59
  • 3
    \$\begingroup\$ Would not a return !value; suffice? \$\endgroup\$ – James Khoury Oct 26 '11 at 1:16
  • 1
    \$\begingroup\$ @MuhammadOmerAslam the OG site apears to be dead but, I added a different article discussing the issue. Hope that helps :P \$\endgroup\$ – Terrance Sep 13 at 12:21
91
\$\begingroup\$

Starting with:

return (!value || value == undefined || value == "" || value.length == 0);

Looking at the last condition, if value == "", it's length MUST be 0. Therefore drop it:

return (!value || value == undefined || value == "");

But wait! In JS, an empty string is false. Therefore, drop value == "":

return (!value || value == undefined);

And !undefined is true, so that check isn't needed. So we have:

return (!value);

And we don't need parentheses:

return !value

Q.E.D.

\$\endgroup\$
  • 5
    \$\begingroup\$ i must say... bravo \$\endgroup\$ – Hexxagonal Oct 31 '11 at 15:43
  • 1
    \$\begingroup\$ @ndp Gotta give it to you. That was complete and concise. Nice. \$\endgroup\$ – Terrance Nov 2 '11 at 20:37
  • \$\begingroup\$ @ndp, Great answer! It's bit confusing for a moment tho... Please add a conclusion to your answer stating it's the final solution, for quick reference, thanks. \$\endgroup\$ – Shimmy Mar 17 '15 at 7:16
  • 1
    \$\begingroup\$ A really Great Answer ndp! I added a screen shot in a reply that shows verfies it and shows execution for those who don't immediately get it. var A; var B = null; var C = "test"; console.log("is A nullOrEmpty %o", !A); console.log("is B nullOrEmpty %o", !B); console.log("is C nullOrEmpty %o", !C); \$\endgroup\$ – Paul Gorbas Sep 24 '18 at 0:01
14
\$\begingroup\$

There are just a few revisions I would make.

First, always use === instead of == in Javascript. You can read more about that on Stack Overflow.

Second, since undefined is mutable, I would reccomend using

typeof value === "undefined"

instead of

value === undefined

Third, I would remove the !value and value === "" conditions. They are redundant.

My Revision

I would use a slightly different approach than you:

String.isNullOrEmpty = function(value) {
  return !(typeof value === "string" && value.length > 0);
}

This checks if the type of the value is "string" (and thus non-null and not undefined), and if it is not empty. If so, it is not null or empty.

Note that this returns true for non-string inputs, which might not be what you want if you wanted to throw an error for an unexpected input type.

\$\endgroup\$
6
\$\begingroup\$

Your function unexpectedly returns true for the following non-string values:

  • false
  • 0
  • Number.NaN
  • [[]]
  • []

It's quite possible that !value though it is similarly sloppy, would suffice, in which case you wouldn't need to define this function at all. But if you create a function that is named isNullOrEmpty, then it should do just that.

function String.isNullOrEmpty(value) {
    return value == null || value === "";
}

Note that value == null is shorthand for value === null || value === undefined.

\$\endgroup\$
  • \$\begingroup\$ This is clean. Using coercion is not evil as many might suggest. As long as you realize you are doing it and how and WHY it works. \$\endgroup\$ – Joe Johnston Mar 7 '18 at 19:04
3
\$\begingroup\$

You should use Object.prototype.isNullOrEmpty = function() { alert(this) }. This ties it to the String object in all instances. This would start to give you access to use strings and variables like "".isNullOrEmpty or var x = null; x.isNullOrEmpty();

If your intent is to use it as a function to pass in variables: String.isNullOrEmpty();

\$\endgroup\$
  • 1
    \$\begingroup\$ Except some people don't like extending the native prototypes. \$\endgroup\$ – Peter Olson Oct 28 '11 at 15:17
  • 4
    \$\begingroup\$ Additionally, the example with null won't work. \$\endgroup\$ – Michal Leszczyk Dec 11 '14 at 9:58
  • \$\begingroup\$ And it doesn't work with undefined either. \$\endgroup\$ – Dehalion Sep 13 at 13:43
3
\$\begingroup\$

Ryan and seand are spot on: This achieves your end.

Object.prototype.isNullOrEmpty = function(value){
    return (!value);
}

This is what I love about JavaScript!

\$\endgroup\$
  • \$\begingroup\$ Just make sure nobody else implements a different isNullOrEmpty in Object.prototype and heavily relies on his implementation. Patching Object.prototype is evil. \$\endgroup\$ – Florian F Nov 24 at 22:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.