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

209
Visualizações
Convert if else statement into ternary multiple lines

I'm confused with the question mark and the colon on ternary can someone explain how to do it correctly

const bill = 220;
const fifty = 50;
const oneHundred = 100;
const twoHundred = 200;

if(bill < fifty)
    console.log(`(A) bill is less than 50`);
else if(bill < oneHundred)
    console.log('(B) bill is greater than 50');
else if(bill < twoHundred)
    console.log('(C) bill is greater than 100');
else if(bill > twoHundred)
    console.log('(D) bill is greater than 200');
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Conditional (ternary) operator

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as an alternative to an if...else statement.

const bill = 220;
const fifty = 50;
const oneHundred = 100;
const twoHundred = 200;

console.log(
  bill < fifty
    ? "(A) bill is less than 50"
    : bill < oneHundred
    ? "(B) bill is greater than 50"
    : bill < twoHundred
    ? "(C) bill is greater than 100"
    : "(D) bill is greater than 200"
);

about 4 years ago · Juan Pablo Isaza Relatório

0

The simple way to explain are see with this form

condition ? (if condition == true) : (if condition == false)

Other thing you neet to now are the ternary "return" the data depending of the condition

let age = 30
let message = age > 18 ? "Can pass" : "Can't pass"
console.log(message) // Output: Can pass
about 4 years ago · Juan Pablo Isaza Relatório

0

You could get a message from a ternary operator.

For better reading (and understanding) the expression is nested for the levels.

const
    bill = 220,
    fifty = 50,
    oneHundred = 100,
    twoHundred = 200,
    message = bill < fifty
        ? '(A) bill is less than 50'
        : bill < oneHundred
            ? '(B) bill is greater than 50'
            : bill < twoHundred
                ? '(C) bill is greater than 100'
                : bill > twoHundred
                ? '(D) bill is greater than 200'
                : '';

console.log(message);

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