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
+9-8Lines changed: 9 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -77,8 +77,8 @@ constant = " I can't be reassigned";
77
77
// Uncaught TypeError: Assignment to constant variable
78
78
```
79
79
80
-
>**Important**:
81
-
This **does not** mean that variables declared with `const` are immutable.
80
+
>**Important**:
81
+
> This **does not** mean that variables declared with `const` are immutable.
82
82
83
83
84
84
@@ -95,11 +95,11 @@ console.log(person.age);
95
95
// 26
96
96
```
97
97
98
-
In this case we are not reassigning the whole variable but just one of its properties, which works fine fine.
98
+
In this case we are not reassigning the whole variable but just one of its properties, which works fine.
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
104
```JavaScript
105
105
constperson= {
@@ -129,12 +129,12 @@ First let's have a look at a simple example:
@@ -147,19 +147,20 @@ Despite what you may read on other sources, both `var` and `let`(and `const`) ar
147
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
153
## When to use `Var`, `Let` and `Const`
153
154
154
155
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
156
156
-
The first opinion comes from [Mathias Bynes:](https://mathiasbynens.be/notes/es6-const)
157
+
The first opinion comes from [Mathias Bynes](https://mathiasbynens.be/notes/es6-const):
157
158
158
159
- Use `const` by default
159
160
- Use `let` only if rebinding is needed.
160
161
-`var` should never be used in ES6.
161
162
162
-
The second opinion comes from [Kyle Simpson:](https://me.getify.com/)
163
+
The second opinion comes from [Kyle Simpson](https://me.getify.com/):
163
164
164
165
- Use `var` for top-level variables that are shared across many (especially larger) scopes.
165
166
- Use `let` for localized variables in smaller scopes.
0 commit comments