Skip to content

Commit f6ea6dc

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

File tree

6 files changed

+102
-89
lines changed

6 files changed

+102
-89
lines changed

js/lesson1/tutorial.md

Lines changed: 26 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,25 @@ 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+
tryChangingMe = 2; // Error! "invalid assignment to const"
200+
```
201+
202+
190203
### Keywords, identifiers, and strings
191204

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

195208
Earlier in this tutorial, we had you try typing `Hello` on the console
196209
without quotes, and you got an error about it not being defined. You
197210
can put anything you like into a string, but things that aren't in a
198211
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
212+
them. The `let` keyword defines a new word to be a variable. In this
200213
section you created the variables `a`, `b`, and `c`, so you could use
201214
those.
202215

@@ -209,11 +222,11 @@ used in identifiers.
209222

210223
The other important rule is that you can't write an identifier that is
211224
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
225+
`let`. If you try writing this then you will get a fairly obscure
213226
error message:
214227

215228
```js
216-
var var = 1;
229+
let let = 1;
217230
```
218231

219232
## Functions
@@ -432,7 +445,7 @@ We apologise for this quirk of the language.
432445
Another type of **value** in JavaScript is an **object**. An object looks like this:
433446

434447
```js
435-
var person = {
448+
const person = {
436449
first_name: "Archibald",
437450
likes: "owls"
438451
};
@@ -461,11 +474,11 @@ variable". This is important because you can, and often will, have
461474
several variables that refer to the same object. Try this:
462475

463476
```js
464-
var person_a = {
477+
const person_a = {
465478
first_name: "Archibald",
466479
likes: "owls"
467480
};
468-
var person_b = person_a;
481+
const person_b = person_a;
469482

470483
console.log("Before");
471484
console.log(person_a.first_name);

0 commit comments

Comments
 (0)