lexical declaration cannot appear in a single-statement context❗️

JSerrorSyntaxError ⟩ lexical declaration cannot appear in a single-statement context

// ⭐ "if" syntax:
// -----------------------------------------------------
//                ╭──⭐───╮ <---- expecting a "statement"
// if (condition) statement;
// -----------------------------------------------------

//        ╭──var──╮      <---- ✅ "var" is a "statement"
if (true) var a = 1;

//        ╭── block ──╮  <---- ✅ "block" is a statement
if (true) { let b = 2 }

// -----------------------------------------------------------------
//     ❗can't use "declaration" when "statement" is expected❗
// -----------------------------------------------------------------
//
//        ╭──let──╮      <---- ❌ "let" is a "declaration"❗
if (true) let c = 2;
//        ^^^
//
// ⛔ SyntaxError: 
//    Lexical declaration cannot appear in a single-statement context

Last updated