Skip to content

Commit 02a4aae

Browse files
committed
Completed
1 parent d03c4cd commit 02a4aae

2 files changed

Lines changed: 202 additions & 27 deletions

File tree

fundamentals/README.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,146 @@ Remember that there is a very big difference between `obj[name]` and `obj["name"
119119

120120
## Functions
121121

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+
function sum(a, b) {
128+
return a + b;
129+
}
130+
```
131+
132+
and
133+
134+
```js
135+
var sum = 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+
function sum(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+
```
122197

123198

124199
## Statements & expressions
125200

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+
function a() { return 4; }
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+
var b = function a() { return 4; }
251+
```
252+
253+
now it is an expression. This is an exceptional situation where something can be a statement or an expression.
254+
255+
### Examples of not-expressions
126256

257+
The following are not expressions:
127258

259+
* `var` -> this is nothing
260+
* `var x;` -> this is a statement
261+
* `+` -> this is only an operator
262+
* `if (a > 4) { return "yes"; } else { return "no"; }`
128263

264+
`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.

fundamentals/exercises.md

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# JavaScript fundamentals - exercices
22

3-
### 1. Given the following code:
3+
### Given the following code:
44

55
```js
66
var s = "Hello";
@@ -12,26 +12,26 @@ Indicate the type of each:
1212

1313
A. `s`
1414
B. `x`
15-
C. `s.toLowerCase()`
16-
D. `s.toLowerCase`
15+
C. `s.toLowerCase( )`
16+
D. `s.toLower Case`
1717
E. `s.length`
1818
F. `l`
1919

20-
### 2. In `var x = 5 + 6;`, what is `+`?
20+
### In `var x = 5 + 6;`, what is `+`?
2121

2222
A. Function
2323
B. Operator
2424
C. Number
2525
D. Aggregator
2626

27-
### 3. In `var x = 5 + 6;`, what is 'var'?
27+
### In `var x = 5 + 6;`, what is 'var'?
2828

2929
A. Variable
3030
B. Keyword
3131
C. Operator
3232
D. Constant
3333

34-
### 4. Given the following code:
34+
### Given the following code:
3535

3636
```js
3737
var x = z[y];
@@ -44,7 +44,7 @@ B. Key
4444
C. Index or key
4545
D. Array
4646

47-
### 5. Given the following code:
47+
### Given the following code:
4848

4949
```js
5050
var y = 1;
@@ -59,7 +59,7 @@ B. Key
5959
C. Index or key
6060
D. Array
6161

62-
### 6. Given the following code:
62+
### Given the following code:
6363

6464
```js
6565
var y = 'length';
@@ -75,40 +75,79 @@ C. Index or key
7575
D. Array
7676

7777

78-
### 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!)
7998

8099
```js
81100
var s = "HackYourFuture";
82101
var i = s.indexOf("Your");
102+
function sum(a, b) { return a + b; }
103+
var s = sum(4, 5);
104+
var r = Math.sqrt(s);
83105
```
84106

85107

86-
87108
## Arrays & objects
88109

89110

90111
## Statements & expressions
91112

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; }`
93124

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?
101126

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
104128

105-
Bonus:
106-
List all *statements* in the code above
107-
List all *expressions* in the code above
129+
### Given the following code:
108130

131+
```js
132+
var s = "Hello".toLowerCase();
133+
var l = s.length;
109134

110-
Write code for the following:
135+
function sum(a, b) {
136+
return a + b;
137+
}
138+
var max = 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+
```
111151

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

Comments
 (0)