0

This is from O'Reilly's Learning JavaScript Design Patterns (by Addy Osmani). The below is what author says about var, let and const.

The difference between var, const and let is that var is a block-scoped variable, const is a block-scoped constant and let is a block-scoped variable that is only accessible within the block it is declared in. Often, you will want to use let but if you want to avoid variable assignment, you can use const instead. Ultimately, use the convention already in the codebase you're using.

I don't understand how can he say var is block scoped? If my understanding is right, block is the pair of parentheses. And the scope of var should be the current execution.

Neon
  • 490
  • 2
  • 9
  • [The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) – Simone Rossaini Nov 25 '22 at 07:07
  • @SimoneRossaini Thanks, but this doesn't answer what I have asked. My question is how can he claim that it is block-scoped. – Neon Nov 25 '22 at 07:08
  • 1
    i don't know this book, opinion based ?! – Simone Rossaini Nov 25 '22 at 07:09
  • 4
    It's a typo which has not been detected before publishing. – Teemu Nov 25 '22 at 07:11
  • @Teemu that is what makes sense to me. I've tweeted to him, let's see if he replies here or there. :) – Neon Nov 25 '22 at 07:16
  • Here's the correction I suggested: The difference between `var` and `const`/`let` is that `var` is a function-scoped variable (usable in the current function), and `const`/`let` is a block-scoped variable (usable only in the current block). Additionally, the difference between `const` and `let` is that `const` represents a constant that cannot be reassigned, while `let` has the same semantic meaning as `var` (but with a different scope). Often, you will want to use `let` but if you want to avoid variable reassignment, you can use `const` instead. – Nick McCurdy Nov 25 '22 at 07:34

0 Answers0