⛔statement expected❗️
can't use "declaration" when expecting "statement"❗️
JS ⟩ grammar ⟩ expecting statement
can't use declaration when expecting statement❗
var is a statement❗ (👉 var is a statement❗️)
let/const is a declaration.
// ⭐ "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 contextLast updated
Was this helpful?