Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9e443f2

Browse files
committedNov 30, 2021
adding variables
1 parent 6524349 commit 9e443f2

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
 

‎00_1_variables.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Variables
2+
Variables store data in the computer's memory (this is a temporal storage).
3+
4+
There are some constraints at the time of naming variables.
5+
6+
1. You cannot use `reserved keywords`, like `new`.
7+
2. They should start with a letter.
8+
3. The only symbol supported is `_`.
9+
4. They are case sensitive. `age` and `Age` are totally different variables.
10+
11+
Something important to consider is given them an appropriate name. There's no "how to" for this, just try to describe its value from "general" to "particular".
12+
13+
14+
15+
16+
We have 3 keywords to declare a variable in JS: `var`, `let` and `const`
17+
The main difference between `var` and `let/const` (ES2015) is that `var` is **function-scoped** (hoisted) and `let/const` are **block-scoped**.
18+
We should use const for values that should not change (constants).
19+
20+
**Quick tip: AVOID using var**
21+
22+
`var` summary:
23+
1. Inside of a function, it will be hoisted to the top of the function.
24+
2. Outside of a function, it will be attached to the `global` context (for example, `window.yourVarName`)
25+
26+
> The main difference between `let` and `const` is that `let` allows us to reassign its value, while `const` persist its initial assignation. For variables that will not change the values they hold, you should use `const`.
27+
28+
Let's jump to a quick example to see this in action.
29+
30+
```js
31+
function hoistingExample(name) {
32+
if (name) {
33+
var x = name;
34+
let y = name;
35+
}
36+
console.log(`With var x is ${x}`)
37+
console.log(`With let y is ${y}`)
38+
}
39+
40+
hoistingExample('Peter');
41+
```
42+
43+
Result:
44+
```
45+
With var x is Peter
46+
ReferenceError: y is not defined
47+
```
48+
49+
**What's going on...?**
50+
The variable declared with the `var` keyword is hoisted, or, raised to the top of the scope (in this case, the function). Remember: `const` and `let` are `block-scoped`.
51+
52+
So, under the hood, this is what the interpreter is doing...
53+
54+
```js
55+
function hoistingExample(name) {
56+
var x;
57+
if (name) {
58+
x = name;
59+
let y = name;
60+
}
61+
console.log(`With var x is ${x}`)
62+
console.log(`With let y is ${y}`)
63+
}
64+
65+
hoistingExample('Peter');
66+
```
67+
68+
... and, it is what is going to be executed at runtime.
69+
70+
But, what happens if we don't pass any argument.
71+
72+
```js
73+
hoistingExample();
74+
```
75+
76+
Result:
77+
78+
```
79+
With var x is undefined
80+
ReferenceError: y is not defined
81+
```
82+
83+
As you can see, the value of `x` will be `undefined`.
84+
Why? Well, the interpreter is going to host the variable declaration to the top of the scope, but, since its value is assigned inside the if statement and we are not meeting the if condition... Te value of `x` will resolve to `undefined`.
85+
86+
87+
We stated that `let/const` are block scoped, or "colloquially" tied to the curly braces `{}`
88+
89+
<!--
90+
hen the variable is stuck in what is known as the temporal dead zone until the variable’s declaration is processed. This behavior prevents variables from being accessed only until after they’ve been declared.
91+
-->
92+
93+
This nested if example could make things clearer...
94+
We are declaring `x` in a 3 levels nested block, however, we have access at the top of the function. So, in each if (or block), we have access to `x` too.
95+
96+
```js
97+
function hoistingExample(name) {
98+
if (true) {
99+
if (true) {
100+
if (true) {
101+
var x = name;
102+
}
103+
}
104+
}
105+
console.log(name);
106+
}
107+
108+
hoistingExample('Peter'); // Peter
109+
```
110+
111+
**var and global scope**
112+
113+
```js
114+
var name1 = 'Peter';
115+
let name2 = 'Paul';
116+
117+
window.name1; // Peter
118+
window.name2; // undefined
119+
```

0 commit comments

Comments
 (0)
Please sign in to comment.