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
+136Lines changed: 136 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,10 +119,146 @@ Remember that there is a very big difference between `obj[name]` and `obj["name"
119
119
120
120
## Functions
121
121
122
+
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*.
123
+
124
+
The following two pieces of code have the exact same result:
125
+
126
+
```js
127
+
functionsum(a, b) {
128
+
return a + b;
129
+
}
130
+
```
131
+
132
+
and
133
+
134
+
```js
135
+
varsum=function (a, b) {
136
+
return a + b;
137
+
}
138
+
```
139
+
140
+
> Note
141
+
>
142
+
> This is not entirely true, as in the second code, the function is "anonymous", i.e. it has no name. But in both cases, you can call the function like this: `sum(4, 5)`.
143
+
144
+
### Parameters & arguments
145
+
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
+
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`".
149
+
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
+
152
+
* A parameter is the name you want to give to the variable that is available inside of the function.
153
+
* An argument is the actual value you want to assign to the parameters when you call the function.
154
+
155
+
A function that "has two parameters" is also said to "take/accept two arguments". But, sometimes you'll hear people say: "the function has two arguments" or "the function takes two parameters". While formally incorrect, you'll know what they mean.
156
+
157
+
### Calling a function on something
158
+
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
+
161
+
```js
162
+
var s =" this is a string ";
163
+
s.trim(); // -> "this is a string"
164
+
```
165
+
166
+
> Note
167
+
>
168
+
> Technically, this means that the string `s` will become the `this` special variable inside of the function.
169
+
170
+
However, there are functions that you don't call on anything:
171
+
172
+
```js
173
+
functionsum(a, b) { return a + b; }
174
+
sum(4, 5); // -> 9
175
+
```
176
+
177
+
Here, you call the function `sum` on nothing.
178
+
179
+
Most built-in functions in JavaScript, like math functions or logging functions, also use the dot:
180
+
181
+
```js
182
+
Math.round(4.5);
183
+
console.log("hello");
184
+
Array.from([1, 2, 3]);
185
+
```
186
+
187
+
Indeed, these functions are also called "on" `Math`, `console`, `Array`, and so on. However, in this case, their purpose is more to group them logically, so here it's not very important to use that terminology. We'd rather say: "call the function `Math.round` with `4.5` as an argument", i.e. we include it in the full name of the methods.
188
+
189
+
It's more when you think about which functions you can call "on" your own variables (strings, arrays, numbers, etc):
190
+
191
+
```js
192
+
myString.trim();
193
+
myArray.slice();
194
+
myNumber.toString();
195
+
...
196
+
```
122
197
123
198
124
199
## Statements & expressions
125
200
201
+
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.
202
+
203
+
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.
204
+
205
+
Similarly, a statement in JavaScript should provide a command by itself. JavaScript-statements are (almost always) terminated by a semicolon.
206
+
207
+
This is a complete statement:
208
+
209
+
```js
210
+
var s ="HackYourFuture";
211
+
```
212
+
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.
214
+
215
+
However, this is not a complete statement:
216
+
217
+
```js
218
+
4+5
219
+
```
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.
222
+
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
+
225
+
### Examples of expressions
226
+
227
+
Here are some examples of expressions. Remember: expressions evaluate into a value, but do not provide a command:
228
+
229
+
*`sum(a, b)`
230
+
*`a`
231
+
*`a > 4 ? "yes" : "no"`
232
+
*`a + b`
233
+
*`a && b || c`
234
+
*`arr.length`
235
+
*`obj["name"]`
236
+
*`[1, 2, 3]`
237
+
*`arr[1]`
238
+
*`[1]` (this is an array with one element!)
239
+
*`function a() { return 4; }`
240
+
241
+
The last one requires a bit of explanation. If you write:
242
+
243
+
```js
244
+
functiona() { return4; }
245
+
```
246
+
247
+
by itself, this is a *statement* (a function declaration statement). However, if you write it as part of a statement, such as:
248
+
249
+
```js
250
+
varb=functiona() { return4; }
251
+
```
252
+
253
+
now it is an expression. This is an exceptional situation where something can be a statement or an expression.
### 6. Explain as precisely as possible (in English) what the following code does, line by line
78
+
### What is the name of these functions?
79
+
80
+
A. `function a() { return true; }`
81
+
B. `var a = function b() { return true; }`
82
+
C. `var c = function () { return true; }`
83
+
84
+
### Write a function that has two parameters called `first` and `second`
85
+
86
+
### Write a function call that passes three arguments.
87
+
88
+
### Write code for the following
89
+
90
+
A. Declare a variable called `x` and initialize it with the string "Hello".
91
+
B. Declare a variable called `y` and initialize it with the property `length` of `x`.
92
+
C. Declare a variable called `z` and initialize it with the result of calling the method `toUpperCase` on `x`
93
+
D. Declare a function called `myFunction`. This function should take two arguments, and should call the second argument with the first argument as its argument. Then, declare a variable called `f` and initialize it with an empty anonymous function, and call `myFunction` with the arguments `10` and `f`.
94
+
95
+
### Explain as precisely as possible (in English) what the following code does, line by line
96
+
97
+
(Tip: it should look like the items in the previous question!)
79
98
80
99
```js
81
100
var s ="HackYourFuture";
82
101
var i =s.indexOf("Your");
102
+
functionsum(a, b) { return a + b; }
103
+
var s =sum(4, 5);
104
+
var r =Math.sqrt(s);
83
105
```
84
106
85
107
86
-
87
108
## Arrays & objects
88
109
89
110
90
111
## Statements & expressions
91
112
92
-
Indicate whether this is an expression or a statement:
113
+
### Indicate for each of these whether it is an expression or a statement:
114
+
115
+
A. `l`
116
+
B. `l = 4;`
117
+
C. `l == 4`
118
+
D. `if (l == 4) { console.log("yes"); }`
119
+
E. `console.log("yes");`
120
+
F. `"yes"`
121
+
G. `console.log(l == 4 ? "yes" : "no")`
122
+
H. `function a() { return 4; }`
123
+
I. `var a = function () { return 4; }`
93
124
94
-
1.`l`
95
-
2.`l = 4;`
96
-
3.`l == 4`
97
-
4.`if (l == 4) { console.log("yes"); }`
98
-
5.`console.log("yes");`
99
-
6.`"yes"`
100
-
7.`console.log(l == 4 ? "yes" : "no")`
125
+
### How can you tell whether something is a statement?
101
126
102
-
How can you tell whether something is a statement?
103
-
How can you tell whether something is an expression
127
+
### How can you tell whether something is an expression
104
128
105
-
Bonus:
106
-
List all *statements* in the code above
107
-
List all *expressions* in the code above
129
+
### Given the following code:
108
130
131
+
```js
132
+
var s ="Hello".toLowerCase();
133
+
var l =s.length;
109
134
110
-
Write code for the following:
135
+
functionsum(a, b) {
136
+
return a + b;
137
+
}
138
+
varmax=function (a, b) {
139
+
return a > b ? a : b;
140
+
}
141
+
142
+
var s1 =sum(4, 5);
143
+
var s2 =4+5;
144
+
145
+
if (s2 == s1) {
146
+
console.log("same");
147
+
} else {
148
+
console.log("not same");
149
+
}
150
+
```
111
151
112
-
1. Declare a variable called `x` and initialize it with the string "Hello".
113
-
2. Declare a variable called `y` and initialize it with the property `length` of `x`.
114
-
3. Declare a variable called `z` and initialize it with the result of calling the method `toUpperCase` on `x`
152
+
A. List all 11 *statements* in the code above
153
+
B. List all 23 *expressions* in the code above (BONUS!)
0 commit comments