I understand the differences in scope between 'var' and 'let' declarations and understand why 'let' can be more preferable as suggested by the respective docs, but why do so many examples in angular and typescript docs still frequently use var? It's hard to see a pattern in why one or another appears in examples.
Asked
Active
Viewed 3,936 times
2
-
1I think you can find a detailed explanation right here: http://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable – Sergey Mell Mar 25 '17 at 19:44
-
1Probably inertia. People are accustomed to using `var` and don't have any overwhelming need to use `let`, so that's what they use. Simple as that. – JLRishe Mar 25 '17 at 19:58
-
I am not sure this is an exact duplicate of the question marked, since it is really asking about why the writers of the documentation included `var` in the documentation, which isn't really what the other question answers. – Judd Franklin Mar 25 '17 at 20:19
1 Answers
2
Two reasons pop to mind. The first is that TypeScript's authors want you to know that there isn't always need for block scoping of a variable, in which case there's nothing wrong with var
per se, as long as it is treated in the TypeScript way - such as by using declare
- as seen in the documentation below.
https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html
The other case, at least in TypeScript's documentation, is when var
pops up to document gotchas and other quirks in JavaScript that TypeScript can help iron out, as well as to show how TypeScript coexists with conventional JavaScript, as seen in the documentation below.
https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html

Judd Franklin
- 570
- 2
- 5
- 16