Commonly used methods for judging empty strings, arrays, and objects in JS

JS determines empty strings, arrays, and objects

empty string

const string = ''
  • !string return true
  • string.length === 0 return true

empty array

const arr =[]
  • arr.length === 0 return true

empty object

const obj ={
    
    }
  • JSON.stringify(obj) === '{}' return true
  • Object.keys(obj).length === 0 return true

==================There are better ways to judge, please leave a message to add
Please add image description
Also attached is a comparison chart of all data types
Insert image description here

Guess you like

Origin blog.csdn.net/Jet_Lover/article/details/126309107