You-Dont-Know-JS icon indicating copy to clipboard operation
You-Dont-Know-JS copied to clipboard

Function parameters are "let" scoped, not "var"?

Open phantomwhale opened this issue 2 years ago • 2 comments


I already searched for this issue

Edition: (2nd)

Book Title: get started

Chapter: 2

https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/get-started/ch2.md#L281

Question:

Forgive me if I'm wrong (because I don't know JS yet!) but it seems that myname, the function parameter, behaves as let declared, not var declared.

Not sure if the same applies to the hello function though, as I've only really seen them defined at the most outer scope of a JS file.

Or did I miss something in the explanation (I've gone through it twice!)

phantomwhale avatar Jul 13 '22 23:07 phantomwhale

The difference between var and let is, var is function scoped. i.e. var can only be used inside a function. on the other hand let is used as block -scope. i.e. it can be accessed inside a block Also, it is important to note that let cannot be redeclared in a program which is a good thing. Prevents confusion. whereas var can be redefined. So, you can use var or let any of these. but make sure whether you have to redefine the variables or not.

you can use refer here - https://www.programiz.com/javascript/let-vs-var

aayuushh365 avatar Jul 18 '22 09:07 aayuushh365

The book text is correct, this is not a mistake.

getify avatar Jul 21 '22 02:07 getify

@aayuushh365 said it all.

cyberGHostJs avatar Aug 14 '22 05:08 cyberGHostJs