|
| 1 | +# REVIEW JavaScript week 2 |
| 2 | + |
| 3 | +## Statements & expressions |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +Similarly, a statement in JavaScript should provide a command by itself. JavaScript-statements are (almost always) terminated by a semicolon. |
| 10 | + |
| 11 | +This is a complete statement: |
| 12 | + |
| 13 | +```js |
| 14 | +let s = "HackYourFuture"; |
| 15 | +``` |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +However, this is not a complete statement: |
| 20 | + |
| 21 | +```js |
| 22 | +4 + 5 |
| 23 | +``` |
| 24 | + |
| 25 | +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. |
| 26 | + |
| 27 | +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. |
| 28 | + |
| 29 | +### Examples of expressions |
| 30 | + |
| 31 | +Here are some examples of expressions. Remember: expressions evaluate into a value, but do not provide a command: |
| 32 | + |
| 33 | +* `sum(a, b)` |
| 34 | +* `a` |
| 35 | +* `a > 4 ? "yes" : "no"` |
| 36 | +* `a + b` |
| 37 | +* `a && b || c` |
| 38 | +* `arr.length` |
| 39 | +* `obj["name"]` |
| 40 | +* `[1, 2, 3]` |
| 41 | +* `arr[1]` |
| 42 | +* `[1]` (this is an array with one element!) |
| 43 | +* `function a() { return 4; }` |
| 44 | + |
| 45 | +The last one requires a bit of explanation. If you write: |
| 46 | + |
| 47 | +```js |
| 48 | +function a() { return 4; } |
| 49 | +``` |
| 50 | + |
| 51 | +by itself, this is a *statement* (a function declaration statement). However, if you write it as part of a statement, such as: |
| 52 | + |
| 53 | +```js |
| 54 | +let b = function a() { return 4; } |
| 55 | +``` |
| 56 | + |
| 57 | +now it is an expression. This is an exceptional situation where something can be a statement or an expression. |
| 58 | + |
| 59 | +### Examples of not-expressions |
| 60 | + |
| 61 | +The following are not expressions: |
| 62 | + |
| 63 | +* `var` -> this is a keyword, see below |
| 64 | +* `var x;` -> this is a statement |
| 65 | +* `+` -> this is only an operator |
| 66 | +* `if (a > 4) { return "yes"; } else { return "no"; }` |
| 67 | + |
| 68 | +`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. |
| 69 | + |
| 70 | +## Functions |
| 71 | + |
| 72 | +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*. |
| 73 | + |
| 74 | +The following two pieces of code have the exact same result: |
| 75 | + |
| 76 | +```js |
| 77 | +function sum(a, b) { |
| 78 | + return a + b; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +and |
| 83 | + |
| 84 | +```js |
| 85 | +let sum = function (a, b) { |
| 86 | + return a + b; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +> Note |
| 91 | +> |
| 92 | +> 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)`. |
| 93 | +
|
| 94 | +### Parameters & arguments |
| 95 | + |
| 96 | +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.) |
| 97 | + |
| 98 | +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`". |
| 99 | + |
| 100 | +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: |
| 101 | + |
| 102 | +* A parameter is the name you want to give to the variable that is available inside of the function. |
| 103 | +* An argument is the actual value you want to assign to the parameters when you call the function. |
| 104 | + |
| 105 | +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. |
| 106 | + |
| 107 | +### Calling a function on something |
| 108 | + |
| 109 | +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: |
| 110 | + |
| 111 | +```js |
| 112 | +let s = " this is a string "; |
| 113 | +s.trim(); // -> "this is a string" |
| 114 | +``` |
| 115 | + |
| 116 | +> Note |
| 117 | +> |
| 118 | +> Technically, this means that the string `s` will become the `this` special variable inside of the function. |
| 119 | +
|
| 120 | +However, there are functions that you don't call on anything: |
| 121 | + |
| 122 | +```js |
| 123 | +function sum(a, b) { return a + b; } |
| 124 | +sum(4, 5); // -> 9 |
| 125 | +``` |
| 126 | + |
| 127 | +Here, you call the function `sum` on nothing. |
| 128 | + |
| 129 | +Most built-in functions in JavaScript, like math functions or logging functions, also use the dot: |
| 130 | + |
| 131 | +```js |
| 132 | +Math.round(4.5); |
| 133 | +console.log("hello"); |
| 134 | +Array.from([1, 2, 3]); |
| 135 | +``` |
| 136 | + |
| 137 | +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. |
| 138 | + |
| 139 | +It's more when you think about which functions you can call "on" your own variables (strings, arrays, numbers, etc): |
| 140 | + |
| 141 | +```js |
| 142 | +myString.trim(); |
| 143 | +myArray.slice(); |
| 144 | +myNumber.toString(); |
| 145 | +... |
| 146 | +``` |
0 commit comments