60
\$\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\$
14
  • 1
    \$\begingroup\$ Aren't the first checks redundant after you've already tried to call toString on value? \$\endgroup\$ Commented Oct 25, 2011 at 19:50
  • \$\begingroup\$ Possibly.... I was thinking if I were to call a "static" String.isNullOrEmpty(). Not sure if I need it. \$\endgroup\$ Commented Oct 25, 2011 at 20:04
  • \$\begingroup\$ That revision is no good, see this and other posts. You've radically changed the meaning of the function. \$\endgroup\$ Commented Oct 25, 2011 at 20:59
  • 3
    \$\begingroup\$ Would not a return !value; suffice? \$\endgroup\$ Commented Oct 26, 2011 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\$ Commented Sep 13, 2019 at 12:21

5 Answers 5

126
\$\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\$
8
  • 9
    \$\begingroup\$ i must say... bravo \$\endgroup\$ Commented Oct 31, 2011 at 15:43
  • 1
    \$\begingroup\$ @ndp Gotta give it to you. That was complete and concise. Nice. \$\endgroup\$ Commented Nov 2, 2011 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\$ Commented Mar 17, 2015 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\$ Commented Sep 24, 2018 at 0:01
  • 1
    \$\begingroup\$ But if someone passes it as a boolean value of false, then it might return the false positive. I think we need to add the check for that as-well. \$\endgroup\$ Commented Apr 18, 2020 at 15:33
17
\$\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\$
9
\$\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\$
1
  • 2
    \$\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\$ Commented Mar 7, 2018 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\$
3
  • 1
    \$\begingroup\$ Except some people don't like extending the native prototypes. \$\endgroup\$ Commented Oct 28, 2011 at 15:17
  • 5
    \$\begingroup\$ Additionally, the example with null won't work. \$\endgroup\$ Commented Dec 11, 2014 at 9:58
  • 1
    \$\begingroup\$ And it doesn't work with undefined either. \$\endgroup\$ Commented Sep 13, 2019 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\$
3
  • \$\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\$ Commented Nov 24, 2019 at 22:53
  • \$\begingroup\$ Doesn't work with null and undefined value \$\endgroup\$ Commented Dec 3, 2020 at 22:35
  • \$\begingroup\$ This is why I hate JavaScript! \$\endgroup\$ Commented Jun 26, 2023 at 9:00

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.