Checking something isEmpty in Javascript?

后端 未结 18 2497
天涯浪人
天涯浪人 2020-12-12 12:38

How can I check if a variable is empty in Javascript? Sorry for the stupid question, but I\'m a newbie in Javascript!

if(response.photo) is empty {
    do so         


        
相关标签:
18条回答
  • 2020-12-12 13:03

    function isEmpty(variable) {
      const type = typeof variable
      if (variable === null) return true
      if (type === 'undefined') return true
      if (type === 'boolean') return false
      if (type === 'string') return !variable
      if (type === 'number') return false
      if (Array.isArray(variable)) return !variable.length
      if (type === 'object') return !Object.keys(variable).length
      return !variable
    }

    0 讨论(0)
  • 2020-12-12 13:04

    Here my simplest solution.

    Inspired by PHP empty function

    function empty(n){
    	return !(!!n ? typeof n === 'object' ? Array.isArray(n) ? !!n.length : !!Object.keys(n).length : true : false);
    }
    
    //with number
    console.log(empty(0));        //true
    console.log(empty(10));       //false
    
    //with object
    console.log(empty({}));       //true
    console.log(empty({a:'a'}));  //false
    
    //with array
    console.log(empty([]));       //true
    console.log(empty([1,2]));    //false
    
    //with string
    console.log(empty(''));       //true
    console.log(empty('a'));      //false

    0 讨论(0)
  • 2020-12-12 13:04

    Check for undefined:

    if (typeof response.photo == "undefined")
    {
        // do something
    }
    

    This would do the equivelant of vb's IsEmpty. If myvar contains any value, even null, empty string, or 0, it is not "empty".

    To check if a variable or property exists, eg it's been declared, though it may be not have been defined, you can use the in operator.

    if ("photo" in response)
    {
        // do something
    }
    
    0 讨论(0)
  • 2020-12-12 13:05

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

    0 讨论(0)
  • 2020-12-12 13:06

    Here's a simpler(short) solution to check for empty variables. This function checks if a variable is empty. The variable provided may contain mixed values (null, undefined, array, object, string, integer, function).

    function empty(mixed_var) {
     if (!mixed_var || mixed_var == '0') {
      return true;
     }
     if (typeof mixed_var == 'object') {
      for (var k in mixed_var) {
       return false;
      }
      return true;
     }
     return false;
    }
    
    //   example 1: empty(null);
    //   returns 1: true
    
    //   example 2: empty(undefined);
    //   returns 2: true
    
    //   example 3: empty([]);
    //   returns 3: true
    
    //   example 4: empty({});
    //   returns 4: true
    
    //   example 5: empty(0);
    //   returns 5: true
    
    //   example 6: empty('0');
    //   returns 6: true
    
    //   example 7: empty(function(){});
    //   returns 7: false
    
    0 讨论(0)
  • 2020-12-12 13:14
    if (myVar == undefined)
    

    will work to see if the var is declared but not initalized.

    0 讨论(0)
提交回复
热议问题