|
| 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 | +## Arrays |
| 61 | + |
| 62 | +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". |
| 63 | + |
| 64 | +When you want to access an element inside an array, you use an "index". This is the number that you put between brackets (`[]`). |
| 65 | + |
| 66 | +Given the following code: |
| 67 | + |
| 68 | +```js |
| 69 | +var arr = ["john", "jane", "jack"]; |
| 70 | +console.log(arr[0]); |
| 71 | +``` |
| 72 | + |
| 73 | +The number `0` is the "index of the first element of array `arr`". Conversely, the element "at index 0 in array `arr` is `'john'`". |
| 74 | + |
| 75 | +Instead of a number, you can also use a variable to access elements in an array, _as long as this variable is a number_: |
| 76 | + |
| 77 | +```js |
| 78 | +var arr = ["john", "jane", "jack"]; |
| 79 | +var a = 1; |
| 80 | +console.log(arr[a]); // -> jane |
| 81 | +``` |
| 82 | + |
| 83 | +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`. |
| 84 | + |
| 85 | +## Objects |
| 86 | + |
| 87 | +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". |
| 88 | + |
| 89 | +```js |
| 90 | +var obj = { name: "John", age: 24 }; |
| 91 | +``` |
| 92 | + |
| 93 | +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`. |
| 94 | + |
| 95 | +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. |
| 96 | + |
| 97 | +```js |
| 98 | +console.log(obj.name); // -> 'John' |
| 99 | +console.log(obj["name"]); // -> 'John' |
| 100 | +``` |
| 101 | + |
| 102 | +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! |
| 103 | + |
| 104 | +```js |
| 105 | +var ageKey = "age"; |
| 106 | +console.log(obj[ageKey]); // -> 24 |
| 107 | +``` |
| 108 | + |
| 109 | +Remember that there is a very big difference between `obj[name]` and `obj["name"]`. |
| 110 | + |
| 111 | +> Note: |
| 112 | +> |
| 113 | +> 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. |
| 114 | +
|
| 115 | +## Functions |
| 116 | + |
| 117 | +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_. |
| 118 | + |
| 119 | +The following two pieces of code have the exact same result: |
| 120 | + |
| 121 | +```js |
| 122 | +function sum(a, b) { |
| 123 | + return a + b; |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +and |
| 128 | + |
| 129 | +```js |
| 130 | +var sum = function (a, b) { |
| 131 | + return a + b; |
| 132 | +}; |
| 133 | +``` |
| 134 | + |
| 135 | +> Note |
| 136 | +> |
| 137 | +> 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)`. |
| 138 | +
|
| 139 | +### Parameters & arguments |
| 140 | + |
| 141 | +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.) |
| 142 | + |
| 143 | +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`". |
| 144 | + |
| 145 | +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: |
| 146 | + |
| 147 | +- A parameter is the name you want to give to the variable that is available inside of the function. |
| 148 | +- An argument is the actual value you want to assign to the parameters when you call the function. |
| 149 | + |
| 150 | +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. |
| 151 | + |
| 152 | +### Calling a function on something |
| 153 | + |
| 154 | +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: |
| 155 | + |
| 156 | +```js |
| 157 | +var s = " this is a string "; |
| 158 | +s.trim(); // -> "this is a string" |
| 159 | +``` |
| 160 | + |
| 161 | +> Note |
| 162 | +> |
| 163 | +> Technically, this means that the string `s` will become the `this` special variable inside of the function. |
| 164 | +
|
| 165 | +However, there are functions that you don't call on anything: |
| 166 | + |
| 167 | +```js |
| 168 | +function sum(a, b) { |
| 169 | + return a + b; |
| 170 | +} |
| 171 | +sum(4, 5); // -> 9 |
| 172 | +``` |
| 173 | + |
| 174 | +Here, you call the function `sum` on nothing. |
| 175 | + |
| 176 | +Most built-in functions in JavaScript, like math functions or logging functions, also use the dot: |
| 177 | + |
| 178 | +```js |
| 179 | +Math.round(4.5); |
| 180 | +console.log("hello"); |
| 181 | +Array.from([1, 2, 3]); |
| 182 | +``` |
| 183 | + |
| 184 | +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. |
| 185 | + |
| 186 | +It's more when you think about which functions you can call "on" your own variables (strings, arrays, numbers, etc): |
| 187 | + |
| 188 | +```js |
| 189 | +myString.trim(); |
| 190 | +myArray.slice(); |
| 191 | +myNumber.toString(); |
| 192 | +... |
| 193 | +``` |
| 194 | + |
| 195 | +## Statements & expressions |
| 196 | + |
| 197 | +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. |
| 198 | + |
| 199 | +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. |
| 200 | + |
| 201 | +Similarly, a statement in JavaScript should provide a command by itself. JavaScript-statements are (almost always) terminated by a semicolon. |
| 202 | + |
| 203 | +This is a complete statement: |
| 204 | + |
| 205 | +```js |
| 206 | +var s = "HackYourFuture"; |
| 207 | +``` |
| 208 | + |
| 209 | +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. |
| 210 | + |
| 211 | +However, this is not a complete statement: |
| 212 | + |
| 213 | +```js |
| 214 | +4 + 5; |
| 215 | +``` |
| 216 | + |
| 217 | +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. |
| 218 | + |
| 219 | +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. |
| 220 | + |
| 221 | +### Examples of expressions |
| 222 | + |
| 223 | +Here are some examples of expressions. Remember: expressions evaluate into a value, but do not provide a command: |
| 224 | + |
| 225 | +- `sum(a, b)` |
| 226 | +- `a` |
| 227 | +- `a > 4 ? "yes" : "no"` |
| 228 | +- `a + b` |
| 229 | +- `a && b || c` |
| 230 | +- `arr.length` |
| 231 | +- `obj["name"]` |
| 232 | +- `[1, 2, 3]` |
| 233 | +- `arr[1]` |
| 234 | +- `[1]` (this is an array with one element!) |
| 235 | +- `function a() { return 4; }` |
| 236 | + |
| 237 | +The last one requires a bit of explanation. If you write: |
| 238 | + |
| 239 | +```js |
| 240 | +function a() { |
| 241 | + return 4; |
| 242 | +} |
| 243 | +``` |
| 244 | + |
| 245 | +by itself, this is a _statement_ (a function declaration statement). However, if you write it as part of a statement, such as: |
| 246 | + |
| 247 | +```js |
| 248 | +var b = function a() { |
| 249 | + return 4; |
| 250 | +}; |
| 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 |
| 256 | + |
| 257 | +The following are not expressions: |
| 258 | + |
| 259 | +- `var` -> this is a keyword, see below |
| 260 | +- `var x;` -> this is a statement |
| 261 | +- `+` -> this is only an operator |
| 262 | +- `if (a > 4) { return "yes"; } else { return "no"; }` |
| 263 | + |
| 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. |
| 265 | + |
| 266 | +### Keywords |
| 267 | + |
| 268 | +Some words in JavaScript are special, e.g. `var`, `if`, `while`, `return`. These are called "keywords". You typically cannot use these words as names for your variables, functions. |
0 commit comments