Checking something isEmpty in Javascript?

后端 未结 18 2496
天涯浪人
天涯浪人 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 12:58

    Empty check on a JSON's key depends on use-case. For a common use-case, we can test for following:

    1. Not null
    2. Not undefined
    3. Not an empty String ''
    4. Not an empty Object {} [] (Array is an Object)

    Function:

    function isEmpty(arg){
      return (
        arg == null || // Check for null or undefined
        arg.length === 0 || // Check for empty String (Bonus check for empty Array)
        (typeof arg === 'object' && Object.keys(arg).length === 0) // Check for empty Object or Array
      );
    }
    

    Return true for:

    isEmpty(''); // Empty String
    isEmpty(null); // null
    isEmpty(); // undefined
    isEmpty({}); // Empty Object
    isEmpty([]); // Empty Array
    
    0 讨论(0)
  • 2020-12-12 13:00

    It depends on what you mean by "empty". The most common pattern is to check to see if the variable is undefined. Many people also do a null check, for example:
    if (myVariable === undefined || myVariable === null)...

    or, in a shorter form:
    if (myVariable || myVariable === null)...

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

    just put the variable inside the if condition, if variable has any value it will return true else false.

    if (response.photo){ // if you are checking for string use this if(response.photo == "") condition
     alert("Has Value");
    }
    else
    {
     alert("No Value");
    };
    
    0 讨论(0)
  • 2020-12-12 13:01

    what am I missing if empty array... keyless object... falseness const isEmpty = o => Array.isArray(o) && !o.join('').length || typeof o === 'object' && !Object.keys(o).length || !(+value);

    0 讨论(0)
  • If you're looking for the equivalent of PHP's empty function, check this out:

    function empty(mixed_var) {
      //   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({'aFunc' : function () { alert('humpty'); } });
      //   returns 5: false
    
      var undef, key, i, len;
      var emptyValues = [undef, null, false, 0, '', '0'];
    
      for (i = 0, len = emptyValues.length; i < len; i++) {
        if (mixed_var === emptyValues[i]) {
          return true;
        }
      }
    
      if (typeof mixed_var === 'object') {
        for (key in mixed_var) {
          // TODO: should we check for own properties only?
          //if (mixed_var.hasOwnProperty(key)) {
          return false;
          //}
        }
        return true;
      }
    
      return false;
    }
    

    http://phpjs.org/functions/empty:392

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

    If you're testing for an empty string:

    if(myVar === ''){ // do stuff };
    

    If you're checking for a variable that has been declared, but not defined:

    if(myVar === null){ // do stuff };
    

    If you're checking for a variable that may not be defined:

    if(myVar === undefined){ // do stuff };
    

    If you're checking both i.e, either variable is null or undefined:

    if(myVar == null){ // do stuff };
    
    0 讨论(0)
提交回复
热议问题