getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
178.98k stars 33.46k forks source link

Update ch2.md #1529

Closed pranithsrujanroy closed 4 years ago

pranithsrujanroy commented 4 years ago

the variable name is a block scoped variable but it's confusing when said it behaves as a variable declared with var

Yes, I promise I've read the Contributions Guidelines


I already searched for this issue

Edition: 2nd

Book Title: You Don't Know JS Yet

Chapter: 2

Section Title:

Topic:

getify commented 4 years ago

Why are you changing this?

pranithsrujanroy commented 4 years ago

It says - Both hello and name act generally as if they were declared with var. but name being a function parameter is block scoped and behaves like a variable declared with let.

In the later example of catch block isn't it the similar case where err is a parameter where it is written is block scoped and behaves as if declared with let

getify commented 4 years ago

but name being a function parameter is block scoped and behaves like a variable declared with let.

This is not accurate. Parameters are available to the entire function, and thus act more like a var. Moreover, you can do var paramName = .. in the function body to "re-declare" a variable, unlike let which would disallow such a re-declaration.

The way in which parameters do act more like a let declaration is if you're using = .. default parameter expressions, in which case you can violate the TDZ (which vars do not have). So parameters are sorta like both, but I would claim they are much more like var, especially historically, than like let.

pranithsrujanroy commented 4 years ago

This is not accurate. Parameters are available to the entire function, and thus act more like a var. Moreover, you can do var paramName = .. in the function body to "re-declare" a variable, unlike let which would disallow such a re-declaration.

then isn't is the same with err in catch block. I see the same scenario over there but there it is written it behaves more like a variable declared withlet