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: fundamentals/README.md
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,10 +8,10 @@ A "variable" is a place where you can store information, such as a string, or a
8
8
9
9
### Variable declaration
10
10
11
-
Variables are "declared" using the `var` keyword:
11
+
Variables are "declared" using the `let` and `const` keywords (or the older `var` keyword):
12
12
13
13
```js
14
-
var x =5;
14
+
let x =5;
15
15
```
16
16
17
17
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.
33
33
To get the type of a variable, use the following code:
34
34
35
35
```js
36
-
var x =5;
37
-
var typeOfX =typeof x; // -> "number"
36
+
let x =5;
37
+
let typeOfX =typeof x; // -> "number"
38
38
```
39
39
40
40
Note that I've put an asterisk behind 'array'. That is because in JavaScript, array is a special kind of object:
41
41
42
42
```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"
45
45
```
46
46
47
47
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
53
53
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.
54
54
55
55
```js
56
-
var x;
56
+
let x;
57
57
console.log(typeof x); // -> "undefined"
58
58
```
59
59
@@ -67,7 +67,7 @@ When you want to access an element inside an array, you use an "index". This is
67
67
Given the following code:
68
68
69
69
```js
70
-
var arr = ['john', 'jane', 'jack'];
70
+
let arr = ['john', 'jane', 'jack'];
71
71
console.log(arr[0]);
72
72
```
73
73
@@ -76,8 +76,8 @@ The number `0` is the "index of the first element of array `arr`". Conversely, t
76
76
Instead of a number, you can also use a variable to access elements in an array, *as long as this variable is a number*:
77
77
78
78
```js
79
-
var arr = ['john', 'jane', 'jack'];
80
-
var a =1;
79
+
let arr = ['john', 'jane', 'jack'];
80
+
let a =1;
81
81
console.log(arr[a]); // -> jane
82
82
```
83
83
@@ -91,7 +91,7 @@ Variables that are objects also contain a list of things, but instead of them be
91
91
92
92
93
93
```js
94
-
var obj = {name:'John', age:24};
94
+
let obj = {name:'John', age:24};
95
95
```
96
96
97
97
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`.
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!
107
107
108
108
```js
109
-
var ageKey ='age';
109
+
let ageKey ='age';
110
110
console.log(obj[ageKey]); // -> 24
111
111
```
112
112
@@ -132,7 +132,7 @@ function sum(a, b) {
132
132
and
133
133
134
134
```js
135
-
varsum=function (a, b) {
135
+
letsum=function (a, b) {
136
136
return a + b;
137
137
}
138
138
```
@@ -145,7 +145,7 @@ var sum = function (a, b) {
145
145
146
146
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.)
147
147
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`".
149
149
150
150
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:
151
151
@@ -159,7 +159,7 @@ A function that "has two parameters" is also said to "take/accept two arguments"
159
159
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:
160
160
161
161
```js
162
-
var s =" this is a string ";
162
+
let s =" this is a string ";
163
163
s.trim(); // -> "this is a string"
164
164
```
165
165
@@ -207,7 +207,7 @@ Similarly, a statement in JavaScript should provide a command by itself. JavaScr
207
207
This is a complete statement:
208
208
209
209
```js
210
-
var s ="HackYourFuture";
210
+
let s ="HackYourFuture";
211
211
```
212
212
213
213
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:
218
218
4+5
219
219
```
220
220
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.
222
222
223
223
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.
224
224
@@ -247,7 +247,7 @@ function a() { return 4; }
247
247
by itself, this is a *statement* (a function declaration statement). However, if you write it as part of a statement, such as:
248
248
249
249
```js
250
-
varb=functiona() { return4; }
250
+
letb=functiona() { return4; }
251
251
```
252
252
253
253
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
0 commit comments