-2

const name = () => {
  b = 12;
  console.log(b);
  let b;
};


const t = () => {
  let c;
  c = 12;
  console.log(c);
};

t();
name();

why name() throws Reference Error compared to t()

after hoisting the name() becomes like t() then why it throws Error

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Hunter003
  • 97
  • 1
  • 1
  • 3
  • You first assign a value (`b=12`), and then at the end tell JS "hey I need you to init a new var" (`let b`). It already exists. You can simply do `let b = 12`. You should ready the `=` as 'be': `let example be 'hey'` -> `let example='hey` – Martijn Aug 15 '23 at 12:32
  • 1
    To make it clear, `var` keyword and implicit global function declaration were early design mistakes and `let` / `const` came to fix. It's confusing because it makes little sense and it causes more bugs than it solves. Luckily we don't have to use it any more. – Álvaro González Aug 16 '23 at 07:17

0 Answers0