How to check if a String is empty in Javascript?

Question:

Given the following object:

var pessoa = {
      nome: "fernando"
}

Note: sometimes I'm receiving the object as follows:

var pessoa = {
      nome: ""
}

How do I check if the name attribute is empty, as shown in the second object?

Answer:

I consider the most practical way:

if (!pessoa.nome)

Well, check it out:

  • vazio ("")
  • null
  • NaN
  • undefined
  • false
  • 0
 var pessoa = { nome: "" } if (!pessoa.nome) console.log("Atributo vazio");
Scroll to Top