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

193
Visualizações
js if else result as condition in another if

I'm wondering if it is possible to use in javascript the result of one if/else statement as condition for another if/else statement. Let me visualize this idea with the code:

if ( if (thatstrue) { return true } else { return false } ) {
 do something
 }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Yes! You can use ternary operator if statement as condition that way:

a = true;

ifFunction = function() {
  if ( true && a ? true : false) {
  console.log('a is true');
  } else {
  console.log('a is false');
  }
}

ifFunction();

a= false;

ifFunction();

I'm giving this Q&A style post becouse i had this problem today in a much more complicated situation with connection with other conditions and couldn't search for a simple and direct answer anywhere. I found a solution by myself hardly reminding myself one line ternary operator and trying to use it. I found it working! Begginners in js mostly use classic if/else conditional blocks (which You can't use that way) and are not familiar with the ternary if/else statement way, which You can simply use as condition in other if else statement. I hope i will help some noob coders like me ;) If i did than please vote me up :D

about 4 years ago · Juan Pablo Isaza Relatório

0

I think the best way to always do this is to abstract the first condition to a function. You can even use it to take parameters and add additional logic iif you need to expand the use case.

var a = 'foobar'

function checkForSomeACondition() {
  return a === myTrueCondition
}


// ...then 

if (checkForSomeACondition()){
  // do something
} else {
  // do something else
}

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