Now before we dive into any features though attached to this lecture you find a link to this page.
This page basically lists all more or less modern javascript features which browsers support these features
and all of which compilers support these features and dare you all to find typescript typescript combined
with core chars which supposedly filled provider but generally describes what typescript is able to
compile.
Now you can always use this resource if you're looking for a specific feature like let's say the cons
keyword.
Where do you want to know is typescript to works with that or not.
Then you can see well how it works with dad and what the restrictions are.
Which browser support is does.
Also by the way gives you a hint regarding which browsers you might want to target.
Regarding the typescript target ends on.
So this is a great resource which allows you to switch between different iterations of JavaScript and
features each iteration introduced and how these features are supported.
So definitely use this as a resource to find out what you can use what you can compile and which target
you might need to set.
And with that let's start with one of the most important and obvious and often used introductions.
Next Gen javascript added by the way with next gen Javascript.
I mean iOS 6 or more recent and that's let or concert what I'm talking about.
It allows you to define constants or variables.
The difference is that constants so variables defined with the cons keyword cannot be changed.
If you would try to assign a new value here even if it has the correct type you'll get an error because
it's a constant.
Now this is by the way an error it is thrown by typescript in vanilla javascript you would get an error
by the browser when you run your code here and that's the idea behind typescript as you learned.
We get an error already wants to writing the code because that of course is better for us as a developer.
It allows us to fix this code earlier before testing it in the console.
So therefore here if we remove that or a comment out this line now it compiles and therefore we get
no error in the browser as well so that's const let is a variable which can be changed and therefore
of course it's similar to var var although still exists because javascript works such that features
aren't suddenly taken away because then older browsers and their scripts might break.
But you shouldn't use var anymore and why should you not use Wah anymore.
I mean it makes sense that we have cons because that really adds a new feature constants.
But why let.
Why did they introduce a new keyword here.
Why didn't they stick to Wah let and cons have one important difference to LA.
Well the obvious difference for constants that the value can't be changed but for let we also get a
difference.
And that's the scope in which this variable is a way lable.
Now you should know this concept of scopes and JavaScript and with var you have global and a function
scope that means that variables that you define outside of functions are available everywhere in your
scripts variables defined inside functions are only available in that function.
For example here if I had a function add and there might get two numbers a and b then we can add a variable
in there with var result.
Let's say and we set result equal to A plus B then and then we return result.
And of course we could have written this in a shorter way but I want to show what more does and this
would be fine.
And the result would only be available instead of dysfunctions if I tried to console log results here
Deadwood failed because it's only available in the scope of this function if I defined it outside of
there however everything works because now it's available globally.
Therefore here I can access it and also the way jobs good works.
You can access variables on a higher level.
So instead of a function you can work with variables scoped to this function but all the variables having
a higher scope like the global scope here so let's Wah now for let the behavior is generally the same.
This works and this works.
So it's generally the same for let but and important differences that for wah.
You really only have global and function scope.
What about if statements.
If we check age greater 20 year and then in there we create a variable is old and set this to true.
Then if I console lock is old here we get an error in typescript but actually would not get one in JavaScript
this code here.
If we grab it just to see that it works and move to the javascript developer tools and there in the
console and we paste it in there and hit enter you see true is getting printed here and indeed if you
try to access is old you get true.
So this is old variable is registered globally and not just in of the if statement because with var
javascript doesn't know any average scopes and functions and global and that changes with let now as
a Z typescript complains nonetheless because this is not good code to write but technically it would
work with let dead changes now with let this really is only available inside of these curly braces because
let them cons to introduce a new concept called block scope which means a variable or constant is always
available in the block in which you define it or in any lower blocks and a block is basically always
a snippet surrounded with currently braces as you have it for a if statement in a for loop in a function
or even that's possible if you just add curly braces like this any variable defined in here would only
be available in that block and that's a good thing because it forces you to write cleaner code and a
wide situations like this.
So just to also proved that there is a difference in the browser if I copy this code using let and I
reload my page here and now enter this year you see I get an error that is old is not defined we didn't
get an error before.
Indeed if I tried to output is old I get that error again.
So that's the difference and therefore let and cause is better not just because we get this constant
feature but also because we are forced to write cleaner code.
So let me comment this out to make it work again.