3 ms·
I always assumed it was to protect against: accidental naming errors, confusion of what a declaration is and copy/paste issues. When I first started writing rus
by shortercode 7y ago
I always assumed it was to protect against: accidental naming errors, confusion of what a declaration is and copy/paste issues. When I first started writing rust and saw it was a thing I thought it was a terrible idea. I'm a more open towards it now, the strong static analysis Rust does help and it can improve code quality if used in small amounts. However, it still can be quite confusing.
Given that JS doesn't restrict the type of a declaration you can just assign a new value to it, place it in a small scope or use a chain.
- heavenlyblue 7y ago>> However, it still can be quite confusing. I don't know - it's never confusing to me. I just use the IDE that allows me to view the types of the variables whenever I need to see them. IDE also highlights the definitions and then the usages of the variable, including the syntax scope where it's used. You're definitely using the wrong tools for the job if you get confused with that little detail. >> Given that JS doesn't restrict the type of a declaration you can just assign a new value to it, place it in a small scope or use a chain. Yeah, but I don't want to semantically assign a new value to the variable. I want this to be a new variable, because it is a new variable. So the point of var is slightly over exaggerated because they could have gone the python way and simply allow declarations to be anything that is an assignment.
- jacobwilliamroy 7y agovar and let tell the compiler the scope in which it should declare the variable (oversimplifying). If var or let are not present, the variable is declared in the global scope; unless you're in strict-mode, then you get a scolding. So var and let are only tangential to the whole declaration process and only indicate the scope in which the variable is bound. I feel confused. Why do you want your assignment statements to be prefixed with var's and let's?
- z3t4 7y agoif you put var in-front you don't have to worry about reassigning a variable from parent scope. You also make assignment and comparison semantically different, example var foo=1; vs foo=1 if(foo=1) and it's thus easier to spot bugs and understand the code.
- z3t4 7y agoWhat IDE (+plugins?) are you using ?