Checking something isEmpty in Javascript?

后端 未结 18 2498
天涯浪人
天涯浪人 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
    }

提交回复
热议问题