|
1 | 1 | # REVIEW JavaScript week 2
|
2 | 2 |
|
3 |
| -## Statements & expressions |
4 |
| - |
5 |
| -Most programming languages that you'll encounter in real life are called "imperative" programming languages. JavaScript is such an imperative programming language. Imperative is another word for command-like. That is, you give the computer a bunch of commands after each other. First do this, then do that, etc. |
6 |
| - |
7 |
| -These individual commands are called "statements" in imperative programming languages. You can compare them with sentences in the English language. They have a use by themselves, and do not need something else. "The man eats bread." is a full sentence, it conveys a meaning by itself. A sentence in English is always terminated by a period. |
8 |
| - |
9 |
| -Similarly, a statement in JavaScript should provide a command by itself. JavaScript-statements are (almost always) terminated by a semicolon. |
10 |
| - |
11 |
| -This is a complete statement: |
12 |
| - |
13 |
| -```js |
14 |
| -let s = "HackYourFuture"; |
15 |
| -``` |
16 |
| - |
17 |
| -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. |
18 |
| - |
19 |
| -However, this is not a complete statement: |
20 |
| - |
21 |
| -```js |
22 |
| -4 + 5 |
23 |
| -``` |
24 |
| - |
25 |
| -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. |
26 |
| - |
27 |
| -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. |
28 |
| - |
29 |
| -### Examples of expressions |
30 |
| - |
31 |
| -Here are some examples of expressions. Remember: expressions evaluate into a value, but do not provide a command: |
32 |
| - |
33 |
| -* `sum(a, b)` |
34 |
| -* `a` |
35 |
| -* `a > 4 ? "yes" : "no"` |
36 |
| -* `a + b` |
37 |
| -* `a && b || c` |
38 |
| -* `arr.length` |
39 |
| -* `obj["name"]` |
40 |
| -* `[1, 2, 3]` |
41 |
| -* `arr[1]` |
42 |
| -* `[1]` (this is an array with one element!) |
43 |
| -* `function a() { return 4; }` |
44 |
| - |
45 |
| -The last one requires a bit of explanation. If you write: |
46 |
| - |
47 |
| -```js |
48 |
| -function a() { return 4; } |
49 | 3 | ```
|
50 |
| - |
51 |
| -by itself, this is a *statement* (a function declaration statement). However, if you write it as part of a statement, such as: |
52 |
| - |
53 |
| -```js |
54 |
| -let b = function a() { return 4; } |
| 4 | +This review covers: |
| 5 | +• Loops (for/while) |
| 6 | +• Functions :white_check_mark: |
| 7 | +• Advanced data types [Objects] :white_check_mark: |
| 8 | +• Conditions |
| 9 | +• Statements vs Expressions :white_check_mark: |
| 10 | +• Naming conventions |
55 | 11 | ```
|
56 | 12 |
|
57 |
| -now it is an expression. This is an exceptional situation where something can be a statement or an expression. |
58 |
| - |
59 |
| -### Examples of not-expressions |
60 |
| - |
61 |
| -The following are not expressions: |
62 |
| - |
63 |
| -* `var` -> this is a keyword, see below |
64 |
| -* `var x;` -> this is a statement |
65 |
| -* `+` -> this is only an operator |
66 |
| -* `if (a > 4) { return "yes"; } else { return "no"; }` |
67 |
| - |
68 |
| -`if` is also a statement. However, it is quite a complex statement. It is also referred to as a "construct", just like `for`, `while`, `try`, etc. |
69 |
| - |
70 | 13 | ## Functions
|
71 | 14 |
|
72 | 15 | A function is a reusable piece of code. Functions are *very* important in JavaScript, to the extent that some people call JavaScript a "function-oriented" language. As mentioned above, variables can be of type function. In fact, *every function is a variable*.
|
@@ -144,3 +87,104 @@ myArray.slice();
|
144 | 87 | myNumber.toString();
|
145 | 88 | ...
|
146 | 89 | ```
|
| 90 | + |
| 91 | + |
| 92 | +## Objects |
| 93 | + |
| 94 | +Variables that are objects also contain a list of things, but instead of them being in some specific order, they can be assigned to words, called "keys". Instead of "elements" the things that are inside objects are called "properties". |
| 95 | + |
| 96 | + |
| 97 | +```js |
| 98 | +var obj = {name: 'John', age: 24}; |
| 99 | +``` |
| 100 | + |
| 101 | +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`. |
| 102 | + |
| 103 | +When accessing object properties, you can use the dot-notation: `obj.name` or the bracket-notation: `obj["name"]`. Note that the latter looks a lot like the way to access array elements. However, here what's inside the bracket (called "key" for objects, instead of "index") must be a string. |
| 104 | + |
| 105 | +```js |
| 106 | +console.log(obj.name); // -> 'John' |
| 107 | +console.log(obj['name']); // -> 'John' |
| 108 | +``` |
| 109 | + |
| 110 | +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! |
| 111 | + |
| 112 | +```js |
| 113 | +var ageKey = 'age'; |
| 114 | +console.log(obj[ageKey]); // -> 24 |
| 115 | +``` |
| 116 | + |
| 117 | +Remember that there is a very big difference between `obj[name]` and `obj["name"]`. |
| 118 | + |
| 119 | +> Note: |
| 120 | +> |
| 121 | +> Thinking back of arrays, the length of an array can be retrieved by `arr.length`. So as mentioned before, arrays are just like other JavaScript objects. You could even write `arr['length']` to access the `length` property of the array. JavaScript will look: is what we put between brackets a number? Then it is an index and we'll look up the correct array element. If it's a string, it's a key and we will look up the corresponding property. |
| 122 | +
|
| 123 | + |
| 124 | +## Statements & expressions |
| 125 | + |
| 126 | +Most programming languages that you'll encounter in real life are called "imperative" programming languages. JavaScript is such an imperative programming language. Imperative is another word for command-like. That is, you give the computer a bunch of commands after each other. First do this, then do that, etc. |
| 127 | + |
| 128 | +These individual commands are called "statements" in imperative programming languages. You can compare them with sentences in the English language. They have a use by themselves, and do not need something else. "The man eats bread." is a full sentence, it conveys a meaning by itself. A sentence in English is always terminated by a period. |
| 129 | + |
| 130 | +Similarly, a statement in JavaScript should provide a command by itself. JavaScript-statements are (almost always) terminated by a semicolon. |
| 131 | + |
| 132 | +This is a complete statement: |
| 133 | + |
| 134 | +```js |
| 135 | +let s = "HackYourFuture"; |
| 136 | +``` |
| 137 | + |
| 138 | +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. |
| 139 | + |
| 140 | +However, this is not a complete statement: |
| 141 | + |
| 142 | +```js |
| 143 | +4 + 5 |
| 144 | +``` |
| 145 | + |
| 146 | +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. |
| 147 | + |
| 148 | +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. |
| 149 | + |
| 150 | +### Examples of expressions |
| 151 | + |
| 152 | +Here are some examples of expressions. Remember: expressions evaluate into a value, but do not provide a command: |
| 153 | + |
| 154 | +* `sum(a, b)` |
| 155 | +* `a` |
| 156 | +* `a > 4 ? "yes" : "no"` |
| 157 | +* `a + b` |
| 158 | +* `a && b || c` |
| 159 | +* `arr.length` |
| 160 | +* `obj["name"]` |
| 161 | +* `[1, 2, 3]` |
| 162 | +* `arr[1]` |
| 163 | +* `[1]` (this is an array with one element!) |
| 164 | +* `function a() { return 4; }` |
| 165 | + |
| 166 | +The last one requires a bit of explanation. If you write: |
| 167 | + |
| 168 | +```js |
| 169 | +function a() { return 4; } |
| 170 | +``` |
| 171 | + |
| 172 | +by itself, this is a *statement* (a function declaration statement). However, if you write it as part of a statement, such as: |
| 173 | + |
| 174 | +```js |
| 175 | +let b = function a() { return 4; } |
| 176 | +``` |
| 177 | + |
| 178 | +now it is an expression. This is an exceptional situation where something can be a statement or an expression. |
| 179 | + |
| 180 | +### Examples of not-expressions |
| 181 | + |
| 182 | +The following are not expressions: |
| 183 | + |
| 184 | +* `var` -> this is a keyword, see below |
| 185 | +* `var x;` -> this is a statement |
| 186 | +* `+` -> this is only an operator |
| 187 | +* `if (a > 4) { return "yes"; } else { return "no"; }` |
| 188 | + |
| 189 | +`if` is also a statement. However, it is quite a complex statement. It is also referred to as a "construct", just like `for`, `while`, `try`, etc. |
| 190 | + |
0 commit comments