How to check empty/undefined/null string in JavaSc

2018-12-31 03:34发布

I saw this thread, 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 for ""?

30条回答
呛了眼睛熬了心
2楼-- · 2018-12-31 04:29

If you need to make sure that the string is not just a bunch of empty spaces (I'm assuming this is for form validation) you need to do a replace on the spaces.

if(str.replace(/\s/g,"") == ""){
}
查看更多
初与友歌
3楼-- · 2018-12-31 04:29

Function:

function is_empty(x)
{
   return ( 
        (typeof x== 'undefined')
                    ||
        (x == null) 
                    ||
        (x == false)  //same as: !x
                    ||
        (x.length == 0)
                    ||
        (x == "")
                    ||
        (x.replace(/\s/g,"") == "")
                    ||
        (!/[^\s]/.test(x))
                    ||
        (/^\s*$/.test(x))
  );
}

p.s. In Javascript, don't use Line-Break after return;

查看更多
骚的不知所云
4楼-- · 2018-12-31 04:29

Ignoring whitespace strings, you could use this to check for null, empty and undefined :

var obj = {};
(!!obj.str) //returns false

obj.str = "";
(!!obj.str) //returns false

obj.str = null;
(!!obj.str) //returns false

Concise and it works for undefined properties, although it's not the most readable.

查看更多
冷夜・残月
5楼-- · 2018-12-31 04:29

Meanwhile we can have one function that checks for all 'empties' like null, undefined, '', ' ', {}, []. So I just wrote this.

var isEmpty = function(data) {
    if(typeof(data) === 'object'){
        if(JSON.stringify(data) === '{}' || JSON.stringify(data) === '[]'){
            return true;
        }else if(!data){
            return true;
        }
        return false;
    }else if(typeof(data) === 'string'){
        if(!data.trim()){
            return true;
        }
        return false;
    }else if(typeof(data) === 'undefined'){
        return true;
    }else{
        return false;
    }
}

Use cases and results.

console.log(isEmpty()); // true
console.log(isEmpty(null)); // true
console.log(isEmpty('')); // true
console.log(isEmpty('  ')); // true
console.log(isEmpty(undefined)); // true
console.log(isEmpty({})); // true
console.log(isEmpty([])); // true
console.log(isEmpty(0)); // false
console.log(isEmpty('Hey')); // false
查看更多
冷夜・残月
6楼-- · 2018-12-31 04:30

The underscore javascript library http://underscorejs.org/ provides a very useful _.isEmpty() function for checking for empty strings and other empty objects.

Reference: http://underscorejs.org/#isEmpty

isEmpty _.isEmpty(object)
Returns true if an enumerable object contains no values (no enumerable own-properties). For strings and array-like objects _.isEmpty checks if the length property is 0.

_.isEmpty([1, 2, 3]);
=> false

_.isEmpty({});
=> true

Other very useful underscore functions include:
http://underscorejs.org/#isNull _.isNull(object)
http://underscorejs.org/#isUndefined _.isUndefined(value)
http://underscorejs.org/#has _.has(object, key)

查看更多
浪荡孟婆
7楼-- · 2018-12-31 04:31

I usually use something like:

if (str == "") {
     //Do Something
}
else {
     //Do Something Else
}
查看更多
登录 后发表回答