You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ebook/01_var_let_const.md
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Chapter 1: Var vs Let vs Const & the temporal dead zone
2
2
3
-
With the introduction of `let` and `const` in **ES6**, we can now better define our variables depending on our needs. During our JavaScript primer we looked at the basic differences between these 3 keywords, now we will go into more detail.
3
+
With the introduction of `let` and `const` in **ES6**, we can now better define our variables depending on our needs. During our `JavaScript` primer we looked at the basic differences between these 3 keywords, now we will go into more detail.
4
4
5
5
6
6
7
7
## `Var`
8
8
9
9
Variables declared with the `var` keyword are **function scoped**, which means that if we declare them inside a `for` loop (which is a **block** scope) they will be available even outside of it.
10
10
11
-
```javascript
11
+
```JavaScript
12
12
for (var i =0; i <10; i++) {
13
13
var leak ="I am available outside of the loop";
14
14
}
@@ -34,7 +34,7 @@ In the first example the value of the `var` leaked out of the block-scope and co
34
34
35
35
Variables declared with the `let` (and `const`) keyword are **block scoped**, meaning that they will be available only inside of the block where they are declared and its sub-blocks.
36
36
37
-
```javascript
37
+
```JavaScript
38
38
// using `let`
39
39
let x ="global";
40
40
@@ -62,29 +62,29 @@ console.log(y);
62
62
// expected output: block-scoped
63
63
```
64
64
65
-
As you can see, when we assigned a new value to our `let` inside our block-scope, it **did not** change its value in the outer scope, whereas when did the same with our `var` it leaked outside of the block-scope and also changed it in the outer scope.
65
+
As you can see, when we assigned a new value to the variable declared with `let` inside our block-scope, it **did not** change its value in the outer scope. Whereas, when we did the same with the variable declared with var, it leaked outside of the block-scope and also changed it in the outer scope.
66
66
67
67
68
68
69
69
## `Const`
70
70
71
-
Similarly to `let`, `const` are **block-scoped**, but they differ in the fact that their value **can't change through re-assignment or can't be re-declared**.
71
+
Similarly to `let`, variables declared with `const` are also**block-scoped**, but they differ in the fact that their value **can't change through re-assignment or can't be re-declared**.
72
72
73
-
```javascript
73
+
```JavaScript
74
74
constconstant='I am a constant';
75
75
constant =" I can't be reassigned";
76
76
77
77
// Uncaught TypeError: Assignment to constant variable
78
78
```
79
79
80
-
**Important**
81
-
This **does not** mean that **const are immutable**.
80
+
>**Important**:
81
+
This **does not** mean that variables declared with `const` are immutable.
82
82
83
83
84
84
85
85
### The content of a `const` is an Object
86
86
87
-
```javascript
87
+
```JavaScript
88
88
constperson= {
89
89
name:'Alberto',
90
90
age:25,
@@ -99,9 +99,9 @@ In this case we are not reassigning the whole variable but just one of its prope
99
99
100
100
---
101
101
102
-
Note: We can still freeze the const object, which will not change the contents of the object (but trying to change the values of object JavaScript will not throw any error)
102
+
>Note: We can still freeze the `const` object, which will not change the contents of the object (but trying to change the values of object `JavaScript` will not throw any error)
103
103
104
-
```javascript
104
+
```JavaScript
105
105
constperson= {
106
106
name:'Alberto',
107
107
age:25,
@@ -144,19 +144,19 @@ let j = "I am a let";
144
144
145
145
Despite what you may read on other sources, both `var` and `let`(and `const`) are subject to **hoisting** which means that they are processed before any code is executed and lifted up to the top of their scope (whether it's global or block).
146
146
147
-
The main differences lays in the fact that `var` can still be accessed before they are defined, causing the value to be `undefined` while on the other hand, `let` variables sit in a **temporal dead zone** until they are declared, causing an error when accessed before initialization which makes it easier to debug code rather than having an `undefined` as the result.
147
+
The main differences lie in the fact that `var` can still be accessed before they are defined. This causes the value to be `undefined`. While on the other hand, `let`lets the variables sit in a **temporal dead zone** until they are declared. And this causes an error when accessed before initialization, which makes it easier to debug code rather than having an `undefined` as the result.
148
148
149
149
---
150
150
151
151
152
152
## When to use `Var`, `Let` and `Const`
153
153
154
-
There is no rule stating where to use each of them and people have different opinions. Here I am going to present to you two opinions from popular developers in the JavaScript community.
154
+
There is no rule stating where to use each of them and people have different opinions. Here I am going to present to you two opinions from popular developers in the `JavaScript` community.
155
155
156
156
The first opinion comes from [Mathias Bynes:](https://mathiasbynens.be/notes/es6-const)
157
157
158
-
-use`const` by default
159
-
-use`let` only if rebinding is needed.
158
+
-Use`const` by default
159
+
-Use`let` only if rebinding is needed.
160
160
-`var` should never be used in ES6.
161
161
162
162
The second opinion comes from [Kyle Simpson:](https://me.getify.com/)
To tell JavaScript what's inside the curly braces is an **object literal** we want to implicitly return, we need to wrap everything inside parenthesis.
81
+
In this example, we are using the `map` function to iterate over the array `runners`. The first argument is the current item in the array and the `i` is the index of it. For each item in the array we are then adding into `results` an Object containing the properties `name`, `race`, and `place`.
82
+
83
+
To tell `JavaScript` what's inside the curly braces is an **object literal** we want to implicitly return, we need to wrap everything inside parenthesis.
82
84
83
85
Writing `race` or `race: race` is the same.
84
86
@@ -90,7 +92,7 @@ As you can see from the previous examples, arrow functions are **anonymous**.
90
92
91
93
If we want to have a name to reference them we can bind them to a variable:
92
94
93
-
```javascript
95
+
```JavaScript
94
96
constgreeting=name=>`hello ${name}`;
95
97
96
98
greeting("Tom");
@@ -106,7 +108,19 @@ When you use an arrow function, the `this` keyword is inherited from the parent
The problem in this case is that the first `this` is bound to the `const` box but the second one, inside the `setTimeout`, will be set to the `Window` object, throwing this error:
We made the argument of our function an Object and when calling the function we don't even have to worry about the order of the parameters because they will be matched based on their key.
92
+
We made the argument of our function an Object. When calling the function, we don’t even have to worry about the order of the parameters because they are matched based on their key.
92
93
93
94
In the example above the default value for *tip* was 0.05 and we overwrote it with 0.15 but we didn't give a value to tax which remained the default 0.1.
94
95
@@ -104,7 +105,7 @@ Notice this detail:
104
105
105
106
If we don't default our argument Object to an empty Object, and we were to try and run `calculatePrice()` we would get:
106
107
107
-
``` javascript
108
+
```JavaScript
108
109
Cannot destructure property `total`of'undefined' or 'null'.
109
110
```
110
111
@@ -121,4 +122,19 @@ calculatePrice(undefined)
121
122
122
123
No matter what we passed, the argument was defaulted to an `Object` which had three default properties of total, tax and tip.
123
124
125
+
```javascript
126
+
functioncalculatePrice({
127
+
total =0,
128
+
tax =0.1,
129
+
tip =0.05}){
130
+
return total + (total * tax) + (total * tip);
131
+
}
132
+
calculatePrice({});
133
+
// cannot read property `total` of 'undefined' or 'null'.
134
+
calculatePrice();
135
+
// cannot read property `total` of 'undefined' or 'null'.
136
+
calculatePrice(undefined)
137
+
// cannot read property `total` of 'undefined' or 'null'.
138
+
```
139
+
124
140
Don't worry about destructuring, we will talk about it in Chapter 10.
0 commit comments