Skip to content

Commit d03c4cd

Browse files
committed
Filling out readme & exercises
1 parent 9141260 commit d03c4cd

2 files changed

Lines changed: 222 additions & 33 deletions

File tree

fundamentals/README.md

Lines changed: 108 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,128 @@
1+
# Understanding JavaScript fundamentals
2+
3+
When talking about JavaScript, it is important that you use the correct terminology. This way, if you talk about code with others, they'll understand what you mean without any ambiguity.
4+
5+
## Variables
6+
7+
A "variable" is a place where you can store information, such as a string, or a number.
8+
9+
### Variable declaration
10+
11+
Variables are "declared" using the `var` keyword:
12+
13+
```js
14+
var x = 5;
15+
```
16+
17+
Here, we say: "declare variable x and initialize it with the number 5".
18+
19+
### Variable types
20+
21+
All variables have a type. In our example above, the variable `x` is a `number`. JavaScript supports the following types:
22+
23+
* `string`, e.g. "HackYourFuture"
24+
* `number`, e.g. 5, or 10.6
25+
* `boolean`, e.g. `true` or `false`
26+
* `array`\*, e.g. `[1, 2, 3]` or `['what', 'is', 'your', 'name']`
27+
* `object`, e.g. `{name: 'John', age: 24}`, or the special object `null`
28+
* `function`, e.g. `function () { return 4; }`
29+
* `symbol`
30+
31+
In addition, a variable may be `undefined`. This is also a special type.
32+
33+
To get the type of a variable, use the following code:
34+
35+
```js
36+
var x = 5;
37+
var typeOfX = typeof x; // -> "number"
38+
```
39+
40+
Note that I've put an asterisk behind 'array'. That is because in JavaScript, array is a special kind of object:
41+
42+
```js
43+
var arr = [1, 2, 3];
44+
var typeOfArr = typeof arr; // -> "object"
45+
```
46+
47+
However, in our communication, we will call these variables arrays.
48+
49+
### Null & undefined
50+
51+
The values `null` and `undefined` are very similar in JavaScript, but they behave a bit differently. The difference is that `null` always has type "object", and `undefined` always has type "undefined".
52+
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+
55+
```js
56+
var x;
57+
console.log(typeof x); // -> "undefined"
58+
```
59+
60+
61+
## Arrays
62+
63+
Variables that are arrays contain a list of things, instead of just one thing. What's inside the array, we typically call "elements". So, the array `[1, 2, 3]` has three elements. The array `[]` has no elements and is therefore empty. The number of elements in an array is called its "length".
64+
65+
When you want to access an element inside an array, you use an "index". This is the number that you put between brackets (`[]`).
66+
167
Given the following code:
268

69+
```js
70+
var arr = ['john', 'jane', 'jack'];
71+
console.log(arr[0]);
372
```
4-
var x = variable[y];
73+
74+
The number `0` is the "index of the first element of array `arr`". Conversely, the element "at index 0 in array `arr` is `'john'`".
75+
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+
78+
```js
79+
var arr = ['john', 'jane', 'jack'];
80+
var a = 1;
81+
console.log(arr[a]); // -> jane
582
```
683

7-
What is `y`:
84+
If the index you use is not an integer (a whole number), or if it's less than `0` or if it's greater than or equal to the array's length, you will get back `undefined`.
885

9-
1. Index
10-
2. Key
11-
3. Index of key
12-
4. Array
1386

1487

88+
## Objects
1589

90+
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".
91+
92+
93+
```js
94+
var obj = {name: 'John', age: 24};
95+
```
96+
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`.
98+
99+
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.
100+
101+
```js
102+
console.log(obj.name); // -> 'John'
103+
console.log(obj['name']); // -> 'John'
16104
```
17-
var s = "Hello";
18-
var x = s.toLowerCase();
19-
var l = s.length;
105+
106+
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+
108+
```js
109+
var ageKey = 'age';
110+
console.log(obj[ageKey]); // -> 24
20111
```
21112

22-
Indicate the type of each:
113+
Remember that there is a very big difference between `obj[name]` and `obj["name"]`.
114+
115+
> Note:
116+
>
117+
> 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.
118+
23119

24-
1. `s`
25-
2. `x`
26-
3. `s.toLowerCase()`
27-
4. `s.toLowerCase`
28-
5. `s.length`
29-
6. `l`
120+
## Functions
30121

31-
Indicate whether this is an expression or a statement:
32122

33-
1. `l`
34-
2. `l = 4;`
35-
3. `l == 4`
36-
4. `if (l == 4) { console.log("yes"); }`
37-
5. `console.log("yes");`
38-
6. `"yes"`
39-
7. `console.log(l == 4 ? "yes" : "no")`
40123

41-
How can you tell whether something is a statement?
42-
How can you tell whether something is an expression
124+
## Statements & expressions
43125

44-
Bonus:
45-
List all *statements* in the code above
46-
List all *expressions* in the code above
47126

48127

49-
Write code for the following:
50128

51-
1. Declare a variable called `x` and initialize it with the string "Hello".
52-
2. Declare a variable called `y` and initialize it with the property `length` of `x`.
53-
3. Declare a variable called `z` and initialize it with the result of calling the method `toUpperCase` on `x`

fundamentals/exercises.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# JavaScript fundamentals - exercices
2+
3+
### 1. Given the following code:
4+
5+
```js
6+
var s = "Hello";
7+
var x = s.toLowerCase();
8+
var l = s.length;
9+
```
10+
11+
Indicate the type of each:
12+
13+
A. `s`
14+
B. `x`
15+
C. `s.toLowerCase()`
16+
D. `s.toLowerCase`
17+
E. `s.length`
18+
F. `l`
19+
20+
### 2. In `var x = 5 + 6;`, what is `+`?
21+
22+
A. Function
23+
B. Operator
24+
C. Number
25+
D. Aggregator
26+
27+
### 3. In `var x = 5 + 6;`, what is 'var'?
28+
29+
A. Variable
30+
B. Keyword
31+
C. Operator
32+
D. Constant
33+
34+
### 4. Given the following code:
35+
36+
```js
37+
var x = z[y];
38+
```
39+
40+
What is `y`:
41+
42+
A. Index
43+
B. Key
44+
C. Index or key
45+
D. Array
46+
47+
### 5. Given the following code:
48+
49+
```js
50+
var y = 1;
51+
var x = [1, 2, 3];
52+
var z = x[y];
53+
```
54+
55+
What is `y`:
56+
57+
A. Index
58+
B. Key
59+
C. Index or key
60+
D. Array
61+
62+
### 6. Given the following code:
63+
64+
```js
65+
var y = 'length';
66+
var x = [1, 2, 3];
67+
var z = x[y];
68+
```
69+
70+
What is `y`:
71+
72+
A. Index
73+
B. Key
74+
C. Index or key
75+
D. Array
76+
77+
78+
### 6. Explain as precisely as possible (in English) what the following code does, line by line
79+
80+
```js
81+
var s = "HackYourFuture";
82+
var i = s.indexOf("Your");
83+
```
84+
85+
86+
87+
## Arrays & objects
88+
89+
90+
## Statements & expressions
91+
92+
Indicate whether this is an expression or a statement:
93+
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")`
101+
102+
How can you tell whether something is a statement?
103+
How can you tell whether something is an expression
104+
105+
Bonus:
106+
List all *statements* in the code above
107+
List all *expressions* in the code above
108+
109+
110+
Write code for the following:
111+
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`

0 commit comments

Comments
 (0)