String.IsNullOrEmpty in JavaScript

  • 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);
    }

    Aren't the first checks redundant after you've already tried to call `toString` on `value`?

    Possibly.... I was thinking if I were to call a "static" String.isNullOrEmpty(). Not sure if I need it.

    That revision is no good, see this and other posts. You've radically changed the meaning of the function.

    Not following you on that second comment. "You've radically changed the meaning of the function" I just removed that first try, catch. I didn't change any comparison operators. Maybe you should give me your thoughts in a full answer form.

    Come to think of it. Because I am going to use it as such, would it be better if if(String.isNullOrEmpty(51354.toString())){} Or should the isNullOrEmpty handle casting

    I'm not really an authoritative source on javascript, I was just trying to comment in general :) I'll try to write something up tomorrow unless someone more knowledgeable turns up. What I meant with my comment was that your two version returns different answers for something like `0` or `false` and undefined variables.

    Would not a `return !value;` suffice?

    @Terrance please take this C# away. `!string` is what you want, `String.isNullOrEmpty(string)` is an abomination

    @Raynos Please elaborate. How do you mean? (How would !string check for empty strings for one and how is String.isNullOrEmpty() an abomination for two) As far as abstraction or performance or what?

    @Terrance `!"" === true` and `!null == true` and `String.isNullOrEmpty` is just bloat that exists for no purpose other then "C# does it, it must be the best"

    @Raynos If it makes you feel better I can certainly change the name of the method. I just needed something that insures that all checks are made in dealing with string validation. And since we are a microsoft shop it seems to make the most sense to use something that would be considered a familiar construct. I could care less that it is a ".NET" thing. As long as the code base doesn't have if(val.length==0) sometimes and other times if(val!=undefined&&val!=null&&val!="") other times. We could call it isNil or isNothing if it insures its usage.

    @Terrance I'm saying what's wrong with `if (!val) {` instead of `if(val === "" || val === null) {`

    the link does not work anymore

    @MuhammadOmerAslam the OG site apears to be dead but, I added a different article discussing the issue. Hope that helps :P

  • ndp

    ndp Correct answer

    9 years ago

    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.


    i must say... bravo

    @ndp Gotta give it to you. That was complete and concise. Nice.

    @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.

    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);

    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.

    What if the value is not a type of string, e.g. a number?

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM