How can I check for an empty/undefined/null string in JavaScript?

后端 未结 30 4772
长发绾君心
长发绾君心 2020-11-21 23:47

I saw this question, but I didn\'t see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking f

30条回答
  •  感动是毒
    2020-11-22 00:03

    I did some research on what happens if you pass a non-string and non-empty/null value to a tester function. As many know, (0 == "") is true in JavaScript, but since 0 is a value and not empty or null, you may want to test for it.

    The following two functions return true only for undefined, null, empty/whitespace values and false for everything else, such as numbers, Boolean, objects, expressions, etc.

    function IsNullOrEmpty(value)
    {
        return (value == null || value === "");
    }
    function IsNullOrWhiteSpace(value)
    {
        return (value == null || !/\S/.test(value));
    }
    

    More complicated examples exists, but these are simple and give consistent results. There is no need to test for undefined, since it's included in (value == null) check. You may also mimic C# behaviour by adding them to String like this:

    String.IsNullOrEmpty = function (value) { ... }
    

    You do not want to put it in Strings prototype, because if the instance of the String-class is null, it will error:

    String.prototype.IsNullOrEmpty = function (value) { ... }
    var myvar = null;
    if (1 == 2) { myvar = "OK"; } // Could be set
    myvar.IsNullOrEmpty(); // Throws error
    

    I tested with the following value array. You can loop it through to test your functions if in doubt.

    // Helper items
    var MyClass = function (b) { this.a = "Hello World!"; this.b = b; };
    MyClass.prototype.hello = function () { if (this.b == null) { alert(this.a); } else { alert(this.b); } };
    var z;
    var arr = [
    // 0: Explanation for printing, 1: actual value
        ['undefined', undefined],
        ['(var) z', z],
        ['null', null],
        ['empty', ''],
        ['space', ' '],
        ['tab', '\t'],
        ['newline', '\n'],
        ['carriage return', '\r'],
        ['"\\r\\n"', '\r\n'],
        ['"\\n\\r"', '\n\r'],
        ['" \\t \\n "', ' \t \n '],
        ['" txt \\t test \\n"', ' txt \t test \n'],
        ['"txt"', "txt"],
        ['"undefined"', 'undefined'],
        ['"null"', 'null'],
        ['"0"', '0'],
        ['"1"', '1'],
        ['"1.5"', '1.5'],
        ['"1,5"', '1,5'], // Valid number in some locales, not in JavaScript
        ['comma', ','],
        ['dot', '.'],
        ['".5"', '.5'],
        ['0', 0],
        ['0.0', 0.0],
        ['1', 1],
        ['1.5', 1.5],
        ['NaN', NaN],
        ['/\S/', /\S/],
        ['true', true],
        ['false', false],
        ['function, returns true', function () { return true; } ],
        ['function, returns false', function () { return false; } ],
        ['function, returns null', function () { return null; } ],
        ['function, returns string', function () { return "test"; } ],
        ['function, returns undefined', function () { } ],
        ['MyClass', MyClass],
        ['new MyClass', new MyClass()],
        ['empty object', {}],
        ['non-empty object', { a: "a", match: "bogus", test: "bogus"}],
        ['object with toString: string', { a: "a", match: "bogus", test: "bogus", toString: function () { return "test"; } }],
        ['object with toString: null', { a: "a", match: "bogus", test: "bogus", toString: function () { return null; } }]
    ];
    

提交回复
热议问题