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

274
Visualizações
Exception handling - Can't catch 'SyntaxError : Missing initializer in const declaration'

I learnt about hoisting in JavaScript , and wanted to experiment the kind of errors i could get , and to catch them. However i can't figure out how to catch this precise error:

SyntaxError : Missing initializer in const declaration.

Here is the piece of code:

try {
    g = 'Hello !';
    console.log(g);
    const g;
  } catch(err) {
    console.log('You got an error '+err);
  }
  console.log('rest of the code.')

I tested this try-catch with other exceptions, and everything seemed to work correctly. So I thought the problem was about hoisting and maybe variables were read right at the start of the block hence were not caught.

I tried to encapsulate the whole code in a try-catch but without any conclusive results. Here is my code encapsulated:

function hoisting() {
  try {
    g = 'Hello !';
    console.log(g);
    const g;
  } catch(err) {
    console.log('You got an error '+err);
  }
  console.log('rest of the code.');
}

function main() {
  try {
  hoisting();
  } catch(err) {
    console.log(err);
  }
}

main();
about 4 years ago · Juan Pablo Isaza
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