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

119
Visualizações
Why some statements can't be used with boolean operators

I like to use this style for single line conditional function calls:

debug && console.log('Debug is on');

It's much more terse than it's if counter part.

However some statements dont work, e.g. return and debugger:

error && return null
(value == '43') && debugger;

Why does it not work for the above statements? Instead I use:

if (error) return null;
if (value == '43') debugger;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

For the same reason you can't say things like these:

const foo = if (bar) { 5 } else { "hi" };
const baz = try { somethingThatCanFail(); } catch (err) { console.error(err); fallback(); };

Javascript, like a lot of other programming languages, retains the difference between an expression and a statement. You can't use a statement in a place where syntactically an expression is expected.

Expressions evaluate to a value, even if that value is undefined. An invocation of a function is an expression. Even an assignment is an expression. But things like try, return, and if are statements.

In some programming languages (notably most functional languages) everything is an expression and evaluates to a value.

about 4 years ago · Juan Pablo Isaza Relatório

0

When you use the boolean operator, it has to be an expression, not a statement. Some of those keywords/operators are only valid full statements

Here is a related question to explain more Why can't we have return in the ternary operator?

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