@@ -141,7 +141,7 @@ Remember:
141141Try typing this on the console:
142142
143143``` js
144- var a = 17 ;
144+ let a = 17 ;
145145```
146146
147147You'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
153153Try 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
160160Now look at the values of b and c. See how it's storing the values of
161161these 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
164164use 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+
166170You 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
170174b = 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
174178variable name, an equals sign, and an expression, then this is a
175179** variable assignment** , which changes the value of a variable that
176180you defined earlier.
@@ -187,16 +191,25 @@ c = a + b;
187191See 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
195208Earlier in this tutorial, we had you try typing ` Hello ` on the console
196209without quotes, and you got an error about it not being defined. You
197210can put anything you like into a string, but things that aren't in a
198211string 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
200213section you created the variables ` a ` , ` b ` , and ` c ` , so you could use
201214those.
202215
@@ -209,11 +222,11 @@ used in identifiers.
209222
210223The other important rule is that you can't write an identifier that is
211224the 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
213226error 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.
432445Another 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
461474several 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
470483console .log (" Before" );
471484console .log (person_a .first_name );
0 commit comments