Skip to content

Commit 535f7c3

Browse files
committed
replaced var by let
1 parent b190b44 commit 535f7c3

3 files changed

Lines changed: 92 additions & 92 deletions

File tree

fundamentals/README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ A "variable" is a place where you can store information, such as a string, or a
88

99
### Variable declaration
1010

11-
Variables are "declared" using the `var` keyword:
11+
Variables are "declared" using the `let` and `const` keywords (or the older `var` keyword):
1212

1313
```js
14-
var x = 5;
14+
let x = 5;
1515
```
1616

1717
Here, we say: "declare variable x and initialize it with the number 5".
@@ -33,15 +33,15 @@ In addition, a variable may be `undefined`. This is also a special type.
3333
To get the type of a variable, use the following code:
3434

3535
```js
36-
var x = 5;
37-
var typeOfX = typeof x; // -> "number"
36+
let x = 5;
37+
let typeOfX = typeof x; // -> "number"
3838
```
3939

4040
Note that I've put an asterisk behind 'array'. That is because in JavaScript, array is a special kind of object:
4141

4242
```js
43-
var arr = [1, 2, 3];
44-
var typeOfArr = typeof arr; // -> "object"
43+
let arr = [1, 2, 3];
44+
let typeOfArr = typeof arr; // -> "object"
4545
```
4646

4747
However, in our communication, we will call these variables arrays.
@@ -53,7 +53,7 @@ The values `null` and `undefined` are very similar in JavaScript, but they behav
5353
Whenever you declare a variable, but you don't set a value, the variable will become `undefined`. JavaScript will never make a variable `null` unless you explicitly program it.
5454

5555
```js
56-
var x;
56+
let x;
5757
console.log(typeof x); // -> "undefined"
5858
```
5959

@@ -67,7 +67,7 @@ When you want to access an element inside an array, you use an "index". This is
6767
Given the following code:
6868

6969
```js
70-
var arr = ['john', 'jane', 'jack'];
70+
let arr = ['john', 'jane', 'jack'];
7171
console.log(arr[0]);
7272
```
7373

@@ -76,8 +76,8 @@ The number `0` is the "index of the first element of array `arr`". Conversely, t
7676
Instead of a number, you can also use a variable to access elements in an array, *as long as this variable is a number*:
7777

7878
```js
79-
var arr = ['john', 'jane', 'jack'];
80-
var a = 1;
79+
let arr = ['john', 'jane', 'jack'];
80+
let a = 1;
8181
console.log(arr[a]); // -> jane
8282
```
8383

@@ -91,7 +91,7 @@ Variables that are objects also contain a list of things, but instead of them be
9191

9292

9393
```js
94-
var obj = {name: 'John', age: 24};
94+
let obj = {name: 'John', age: 24};
9595
```
9696

9797
This object has two properties: `name` and `age`. The "value" of the property `name` is the string `'John'`. The "value" of the property `age` is the number `24`.
@@ -106,7 +106,7 @@ console.log(obj['name']); // -> 'John'
106106
Just like with arrays, you can also use a variable to access properties, as long as these variables are strings. In this case you cannot use the dot-notation!
107107

108108
```js
109-
var ageKey = 'age';
109+
let ageKey = 'age';
110110
console.log(obj[ageKey]); // -> 24
111111
```
112112

@@ -132,7 +132,7 @@ function sum(a, b) {
132132
and
133133

134134
```js
135-
var sum = function (a, b) {
135+
let sum = function (a, b) {
136136
return a + b;
137137
}
138138
```
@@ -145,7 +145,7 @@ var sum = function (a, b) {
145145

146146
When writing `function sum(a, b)`, `a` and `b` are the "parameters" of the function. We say that this function has two parameters. (Sometimes, you'll see the word "arity": this function has "arity" 2, but that is something you don't have to use for now.)
147147

148-
Now, when *calling* function sum, e.g. `var s = sum(4, 5);`, we say that the numbers `4` and `5` are the "arguments" of the function. Arguments are "passed" to the function: "we pass `4` and `5` to the function `sum`".
148+
Now, when *calling* function sum, e.g. `let s = sum(4, 5);`, we say that the numbers `4` and `5` are the "arguments" of the function. Arguments are "passed" to the function: "we pass `4` and `5` to the function `sum`".
149149

150150
So remember the difference between the word "parameter" and "argument". Many people confuse them, and that's not a big problem, but understanding the difference is always nice:
151151

@@ -159,7 +159,7 @@ A function that "has two parameters" is also said to "take/accept two arguments"
159159
In JavaScript, you can call functions *on* something. By this, we mean that you use the dot to call the function. For instance, when we say "call method `trim` on string `s`", we mean:
160160

161161
```js
162-
var s = " this is a string ";
162+
let s = " this is a string ";
163163
s.trim(); // -> "this is a string"
164164
```
165165

@@ -207,7 +207,7 @@ Similarly, a statement in JavaScript should provide a command by itself. JavaScr
207207
This is a complete statement:
208208

209209
```js
210-
var s = "HackYourFuture";
210+
let s = "HackYourFuture";
211211
```
212212

213213
It is a full command: declare a variable `s` and initialize it with `"HackYourFuture"`. JavaScript doesn't need any other information to know what we want. The statement is terminated with a semicolon.
@@ -218,7 +218,7 @@ However, this is not a complete statement:
218218
4 + 5
219219
```
220220

221-
This equals `9`, but what is JavaScript to do with it? It doesn't provide a command. You'd need to do something with it, e.g. `var x = 4 + 5;` or `callFunction(4 + 5)`. We call these parts of statements "expressions". Expressions are not terminated by semicolons. Expressions always "evaluate into a value". In our example, the expression `4 + 5` "evaluates into `9`". If expressions cannot be evaluated into a value, they are invalid. For instance, `4 +` is not a valid expression, it is incomplete, because we need something else after the plus sign.
221+
This equals `9`, but what is JavaScript to do with it? It doesn't provide a command. You'd need to do something with it, e.g. `let x = 4 + 5;` or `callFunction(4 + 5)`. We call these parts of statements "expressions". Expressions are not terminated by semicolons. Expressions always "evaluate into a value". In our example, the expression `4 + 5` "evaluates into `9`". If expressions cannot be evaluated into a value, they are invalid. For instance, `4 +` is not a valid expression, it is incomplete, because we need something else after the plus sign.
222222

223223
So, statements can *contain* expressions. Can expressions contain statements? No, they cannot. However, they can themselves contain expressions. Think about `4 + 5`: it contains the expressions `4` and `5`, as these both evaluate into a value: the expression `4` evaluates into the number `4`, it is a very simple expression. Similarly, `true`, `null`, `undefined` are all expressions.
224224

@@ -247,7 +247,7 @@ function a() { return 4; }
247247
by itself, this is a *statement* (a function declaration statement). However, if you write it as part of a statement, such as:
248248

249249
```js
250-
var b = function a() { return 4; }
250+
let b = function a() { return 4; }
251251
```
252252

253253
now it is an expression. This is an exceptional situation where something can be a statement or an expression.
@@ -256,8 +256,8 @@ now it is an expression. This is an exceptional situation where something can be
256256

257257
The following are not expressions:
258258

259-
* `var` -> this is a keyword, see below
260-
* `var x;` -> this is a statement
259+
* `let` -> this is a keyword, see below
260+
* `let x;` -> this is a statement
261261
* `+` -> this is only an operator
262262
* `if (a > 4) { return "yes"; } else { return "no"; }`
263263

fundamentals/bug-challenge-es6/bug-challenge.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default class BugChallenge {
7070

7171
bug4() {
7272
// We list all movies, except the top 3.
73-
var index = 3;
73+
let index = 3;
7474
for (index; index < this.top10Movies.length; index++) {
7575
console.log(`movie: ${this.top10Movies[index]}`);
7676
}
@@ -125,7 +125,7 @@ export default class BugChallenge {
125125
}
126126

127127
bug8() {
128-
for (var i = 0; i < 5; i++) {
128+
for (let i = 0; i < 5; i++) {
129129
setTimeout(function () {
130130
console.log(i+1);
131131
}, 100*i);
@@ -205,7 +205,7 @@ export default class BugChallenge {
205205
}
206206

207207
bug13() {
208-
var notInTop10 = (movieName) => {
208+
let notInTop10 = (movieName) => {
209209
return !this.top10Movies.indexOf(movieName)
210210
}
211211
console.log('Independence Day is ' + (notInTop10('Independence Day')?'not ':'') + 'in the top 10!');
@@ -217,20 +217,20 @@ export default class BugChallenge {
217217

218218
console.log('AI is ' + (isInFirstPlace('AI')?'':'not ') + 'best movie ever')
219219
console.log('Godfather is ' + (isInFirstPlace('Godfather')?'':'not ') + 'best movie ever')
220-
var isInFirstPlace = (movieName) => {
220+
let isInFirstPlace = (movieName) => {
221221
return this.top10Movies[0] === movieName
222222
}
223223
}
224224
bug15() {
225-
var getAlphabeticalFirst = function() {
225+
let getAlphabeticalFirst = function() {
226226
return this.top10Actors.sort()[0]
227227
}
228228

229229
console.log(`The first actor when sorted alphabetically is ${getAlphabeticalFirst()}`)
230230
}
231231
bug16() {
232232
const ranking = this.top10Actors.indexOf('Al Pacino');
233-
// var thirdRankedActor = this.top10Actors['2'];
233+
// let thirdRankedActor = this.top10Actors['2'];
234234
console.log(`Al Pacino is ranked ${ranking + '1'}`)
235235
}
236236

0 commit comments

Comments
 (0)