Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!