|
| 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 | + |
1 | 67 | Given the following code: |
2 | 68 |
|
| 69 | +```js |
| 70 | +var arr = ['john', 'jane', 'jack']; |
| 71 | +console.log(arr[0]); |
3 | 72 | ``` |
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 |
5 | 82 | ``` |
6 | 83 |
|
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`. |
8 | 85 |
|
9 | | -1. Index |
10 | | -2. Key |
11 | | -3. Index of key |
12 | | -4. Array |
13 | 86 |
|
14 | 87 |
|
| 88 | +## Objects |
15 | 89 |
|
| 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' |
16 | 104 | ``` |
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 |
20 | 111 | ``` |
21 | 112 |
|
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 | +
|
23 | 119 |
|
24 | | -1. `s` |
25 | | -2. `x` |
26 | | -3. `s.toLowerCase()` |
27 | | -4. `s.toLowerCase` |
28 | | -5. `s.length` |
29 | | -6. `l` |
| 120 | +## Functions |
30 | 121 |
|
31 | | -Indicate whether this is an expression or a statement: |
32 | 122 |
|
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")` |
40 | 123 |
|
41 | | -How can you tell whether something is a statement? |
42 | | -How can you tell whether something is an expression |
| 124 | +## Statements & expressions |
43 | 125 |
|
44 | | -Bonus: |
45 | | -List all *statements* in the code above |
46 | | -List all *expressions* in the code above |
47 | 126 |
|
48 | 127 |
|
49 | | -Write code for the following: |
50 | 128 |
|
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` |
|
0 commit comments