Commit 648a9591 authored by colmoneill's avatar colmoneill
Browse files

Update 05A-scope-vars-lets-consts/05A-Scope-in-Java.md

parent 74b7eebf
......@@ -36,6 +36,57 @@ console.log(greeter) // "say Hello instead"
## `let` to the rescue
let is superior to var because it is what we call block scoped
`let` is superior to var because it is what we call *block scoped*
a block is anything that is contained between `{}` so, conditionals, function declarations, ...
example below:
```
let greeting = "say Hi";
let times = 4;
if (times > 3) {
let hello = "say Hello instead";
console.log(hello);// "say Hello instead"
}
console.log(hello) // hello is not defined
```
`let` can be updated but not re-declared; example below:
```
// this will work and is considered legal
let trick = "using lets in js makes you look up to date";
trick = "using vars is ok too sometimes";
// this won't work and will throw an error
let trick = "using lets in js makes you look up to date";
let trick = "other things going on";
```
but you can re-declare `let` in different scopes and you won't have any errors:
```
let greeting = "say Hi";
if (true) {
let greeting = "say Hello instead";
console.log(greeting); // "say Hello instead"
}
console.log(greeting); // "say Hi"
```
↑ the above is the demonstration of the advantage of `let` over `var` as when you use `let` you don't have to worry about variable existing already and possibly being re-declared.
* `let` is block scoped or block contained *
## `const` is for constants
In addition to `let`, you can use a `const` for a variable that has a value that will not change.
const is also block scoped so it can be declared for specific use
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment