Skip to content

Commit efba16f

Browse files
committed
Change JS var usage to let or const
1 parent 87a5668 commit efba16f

File tree

6 files changed

+103
-89
lines changed

6 files changed

+103
-89
lines changed

js/lesson1/tutorial.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Remember:
141141
Try typing this on the console:
142142

143143
```js
144-
var a = 17;
144+
let a = 17;
145145
```
146146

147147
You'll see that it returns the value as `undefined`, undefined
@@ -153,24 +153,28 @@ Now try typing `a` on the console. You should see that you get back 17. Variable
153153
Try typing these:
154154

155155
```js
156-
var b = 12;
157-
var c = a + b;
156+
let b = 12;
157+
let c = a + b;
158158
```
159159

160160
Now look at the values of b and c. See how it's storing the values of
161161
these expressions?
162162

163-
A line starting with `var` is a **variable definition**. You should
163+
A line starting with `let` is a **variable definition**. You should
164164
use this whenever you are creating a new variable.
165165

166+
> In previous versions of JavaScript, `var` was used for all
167+
**variable defintions**, but `let` is now considered better to use.
168+
[You can read more about why on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#description).
169+
166170
You can also change the value of a variable. That's why it's a
167171
"variable": because its value can vary. Try this:
168172

169173
```js
170174
b = 2;
171175
```
172176

173-
Notice that you don't have `var` this time. If you have a
177+
Notice that you don't have `let` this time. If you have a
174178
variable name, an equals sign, and an expression, then this is a
175179
**variable assignment**, which changes the value of a variable that
176180
you defined earlier.
@@ -187,16 +191,26 @@ c = a + b;
187191
See that it's changed this time? This is because the **expression**
188192
`a + b` is converted into its **value** immediately when it is used.
189193

194+
If you want a **value** that cannot be changed, you would use
195+
**const**. These types of variables are called **constants**.
196+
197+
```js
198+
const tryChangingMe = 1
199+
200+
tryChangingMe = 2 // Error! "invalid assignment to const"
201+
```
202+
203+
190204
### Keywords, identifiers, and strings
191205

192-
The word `var` is a **keyword**: a word that means something special
193-
to the language. `var` means "create a new variable".
206+
The word `let` is a **keyword**: a word that means something special
207+
to the language. `let` means "create a new variable".
194208

195209
Earlier in this tutorial, we had you try typing `Hello` on the console
196210
without quotes, and you got an error about it not being defined. You
197211
can put anything you like into a string, but things that aren't in a
198212
string need to be defined to mean something before you can use
199-
them. The `var` keyword defines a new word to be a variable. In this
213+
them. The `let` keyword defines a new word to be a variable. In this
200214
section you created the variables `a`, `b`, and `c`, so you could use
201215
those.
202216

@@ -209,11 +223,11 @@ used in identifiers.
209223

210224
The other important rule is that you can't write an identifier that is
211225
the same as a keyword, so you cannot create a variable called
212-
`var`. If you try writing this then you will get a fairly obscure
226+
`let`. If you try writing this then you will get a fairly obscure
213227
error message:
214228

215229
```js
216-
var var = 1;
230+
let let = 1;
217231
```
218232

219233
## Functions
@@ -432,7 +446,7 @@ We apologise for this quirk of the language.
432446
Another type of **value** in JavaScript is an **object**. An object looks like this:
433447

434448
```js
435-
var person = {
449+
const person = {
436450
first_name: "Archibald",
437451
likes: "owls"
438452
};
@@ -461,11 +475,11 @@ variable". This is important because you can, and often will, have
461475
several variables that refer to the same object. Try this:
462476

463477
```js
464-
var person_a = {
478+
const person_a = {
465479
first_name: "Archibald",
466480
likes: "owls"
467481
};
468-
var person_b = person_a;
482+
const person_b = person_a;
469483

470484
console.log("Before");
471485
console.log(person_a.first_name);

0 commit comments

Comments
 (0)