Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
JS - check if a value defined and not something

I want to have a simple check if a value exists but is not something, that something in my case is the string none. in my rest call (which I can't control) I have an inner object that has the status

{
   name: "some name",
   internal: { // can be null of an object
      status: "my status" // can be "none" which means I can't find the status
   }
}

I want to check for both, but so far I could only come up with

a = obj?.internal?.status
if (a && a !== "none") {
  // code
}

is there a better way to structure my if statement or my code at all?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could do something like this:

if((obj?.internal?.status || "none") !== "none") {
    // code
}

Or you could use the nullish coalescing operator, as jsejcksn says, to make the condition true in case status is an empty string ("").

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the Nullish coalescing operator (??):

const status = obj?.internal?.status ?? 'none';

if (status !== 'none') {
  // use the status
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda