1

Ran across some interesting differences between docs on this very common question.

According to the TS docs, variables declared with varwill leak out of the containing function, but on MSDN it states the var data will not leak and instead is accessible throughout the containing function. This may be just a difference in how Typescript handles var compared to how ECMAScript does, looking to SO for feedback on this.

According to the TS docs

Block-scoping When a variable is declared using let, it uses what some call lexical-scoping or block-scoping. Unlike variables declared with var whose scopes leak out to their containing function, block-scoped variables are not visible outside of their nearest containing block or for-loop.

But according to the MSDN docs

Variables declared by let have their scope in the block for which they are defined, as well as in any contained sub-blocks. In this way, let works very much like var. The main difference is that the scope of a var variable is the entire enclosing function:

2 Answers 2

4

These are two different ways of saying the same thing

The doc says (emphasis mine):

variables declared with var whose scopes leak out to their containing function

which is different from saying

variables declared with var whose scopes leak out of their containing function

Sign up to request clarification or add additional context in comments.

1 Comment

your're totally right, missed that small detail. will accept this when I am able too!
1

TypeScript is transcompiled to JavaScript. When you look at the JavaScript output generated from your TypeScript code, you will see that a variable declared using the keyword var is simply declared the same way.

So the scope is the also same: the variable is accessible in the entire containing function. This is sometimes referred to as hoisting.

1 Comment

Hoisting is a related but different concept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.