|
| 1 | +<!-- .slide: id=" " --> |
| 2 | + |
| 3 | +### JavaScript Course - Fall 2025 |
| 4 | + |
| 5 | +Lesson 2, Thursday, 2025-09-18 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +### Recap |
| 10 | + |
| 11 | +What did we talk about last lesson? |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +### Recap: Javascript Console |
| 16 | + |
| 17 | +`F12` is the magic key |
| 18 | + |
| 19 | +<!-- .slide: style="font-size:80%" --> |
| 20 | + |
| 21 | +- Alternatively, `opt + cmd + C` (Mac) / `ctrl + shift + C` (Windows) |
| 22 | + |
| 23 | +- Or right click on browser viewport and choose "Inspect" |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +### Recap: data types |
| 28 | + |
| 29 | +Number: |
| 30 | + |
| 31 | +```js |
| 32 | +42 |
| 33 | + |
| 34 | +3.1415 |
| 35 | +``` |
| 36 | + |
| 37 | +We can also do computation, using operators: |
| 38 | + |
| 39 | +<!-- .element: class="fragment" --> |
| 40 | + |
| 41 | +```js |
| 42 | +1 + 1 |
| 43 | + |
| 44 | +10 / 5 |
| 45 | + |
| 46 | +5 ** 3 |
| 47 | +``` |
| 48 | + |
| 49 | +<!-- .element: class="fragment" --> |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +### Recap: data types |
| 54 | + |
| 55 | +String: |
| 56 | + |
| 57 | +```js |
| 58 | +"Alan" |
| 59 | + |
| 60 | +'Berlin' |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | +<!-- .element: class="fragment" --> |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +<!-- .slide: id="variables" --> |
| 69 | + |
| 70 | +# Variables |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### Variable |
| 75 | + |
| 76 | +A variable is a pointer to value |
| 77 | + |
| 78 | +```js |
| 79 | +let price = 5; |
| 80 | +let name = "John"; |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +<!-- .element: style="text-align:center; height: 400px" --> |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +### Examples: |
| 90 | + |
| 91 | +```js |
| 92 | +let priceCoffee = 2; |
| 93 | +let priceCappuccino = 3; |
| 94 | +let customer = "John"; |
| 95 | +let greeting = "Hello"; |
| 96 | +let likesCarlo = true; |
| 97 | +let hasKids = false; |
| 98 | +``` |
| 99 | + |
| 100 | +Can you guess how we can define a variable? |
| 101 | + |
| 102 | +<!-- .element: class="fragment" --> |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +### How do we define a variable? |
| 107 | + |
| 108 | +We write: |
| 109 | + |
| 110 | +- `let` |
| 111 | +<!-- .element: class="fragment" --> |
| 112 | +- the variable name |
| 113 | +<!-- .element: class="fragment" --> |
| 114 | +- `=` |
| 115 | +<!-- .element: class="fragment" --> |
| 116 | +- the value we want |
| 117 | +<!-- .element: class="fragment" --> |
| 118 | +- `;` |
| 119 | +<!-- .element: class="fragment" --> |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +### Semicolon in JavaScript |
| 124 | + |
| 125 | +Adding semicolon after statements in JavaScript is optional. |
| 126 | + |
| 127 | +It's often considered good practice and can prevent unexpected behavior. All code in this course uses semicolons. |
| 128 | + |
| 129 | +<!-- .element: class="fragment" --> |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +### Variables are variable |
| 134 | + |
| 135 | +You can change the value of a variable using the assignment operator (`=`): |
| 136 | + |
| 137 | +<!-- .element: class="fragment" --> |
| 138 | + |
| 139 | +```js |
| 140 | +let temperature = 24; // define a variable, initialize it to 24 |
| 141 | +temperature = 30; // assign a new value (30) to the variable |
| 142 | +temperature = 23; // now temperature is 23 |
| 143 | +``` |
| 144 | + |
| 145 | +<!-- .element: class="fragment" --> |
| 146 | + |
| 147 | +We define a variable once, then we can change it as often as needed. |
| 148 | + |
| 149 | +<!-- .element: class="fragment" --> |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +### try it! |
| 154 | + |
| 155 | +<!-- .slide: style="font-size:80%" --> |
| 156 | + |
| 157 | +Create some variables: |
| 158 | + |
| 159 | +- one for your full name |
| 160 | +- one for your age |
| 161 | +- one for your favorite city |
| 162 | +- one for whether you can speak German or not |
| 163 | +- and anything else you can think of :) |
| 164 | + |
| 165 | +In the JavaScript console, enter the name of the variable. Do you see its value? |
| 166 | + |
| 167 | +Bonus: Change the value of one of the variables that you defined. |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +### Variables |
| 172 | + |
| 173 | +Variables can be used wherever we can use values |
| 174 | + |
| 175 | +```js |
| 176 | +let pricePerTicket = 8; |
| 177 | +let friends = 3; |
| 178 | +let totalPrice = friends * pricePerTicket; |
| 179 | +``` |
| 180 | + |
| 181 | +<!-- .element: class="fragment" --> |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +### Why do we use variables? |
| 186 | + |
| 187 | +We use variables: |
| 188 | + |
| 189 | +- to give names (meaning) to values. "42" could mean a person’s age or shoe size. |
| 190 | +<!-- .element: class="fragment" --> |
| 191 | +- for reuse and flexibility. We define the variable once with a value, and use it more than once. |
| 192 | +<!-- .element: class="fragment" --> |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +### Variable names |
| 197 | + |
| 198 | +- You can use letters, numbers, and underscore `_` (spaces are _not_ allowed!) |
| 199 | +- A variable name cannot start with a number |
| 200 | + |
| 201 | +Valid variable names: |
| 202 | + |
| 203 | +```js |
| 204 | +let element = 3; |
| 205 | +let element3 = 5; |
| 206 | +``` |
| 207 | + |
| 208 | +Invalid variable names: |
| 209 | + |
| 210 | +```js |
| 211 | +let 2squared = 4; |
| 212 | +let element-1 = 0; |
| 213 | +let full name = 'Anakin Skywalker'; |
| 214 | +``` |
| 215 | + |
| 216 | +--- |
| 217 | + |
| 218 | +Giving good and descriptive names to your variables is very important! |
| 219 | + |
| 220 | +Good variable names make the code easier to understand by other developers, and even by yourself! |
| 221 | + |
| 222 | +Valid but not very descriptive variable names: |
| 223 | + |
| 224 | +```js |
| 225 | +let a = 0; |
| 226 | +let _12 = 0; |
| 227 | +let asldjf = 0; |
| 228 | +let thisisareallylongvariablename = 0; |
| 229 | +``` |
| 230 | + |
| 231 | +--- |
| 232 | + |
| 233 | +In this course, and JavaScript in general, we use 'camelCase': |
| 234 | + |
| 235 | +- `isStudent` |
| 236 | +- `favoriteBook` |
| 237 | +- `likesGermanFood` |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +Variable names are case sensitive: |
| 242 | + |
| 243 | +```js |
| 244 | +let name = "John"; |
| 245 | +let Name = "John"; |
| 246 | +let NAME = "John"; |
| 247 | +``` |
| 248 | + |
| 249 | +These are 3 different variables. |
| 250 | + |
| 251 | +--- |
| 252 | + |
| 253 | +### Practice |
| 254 | + |
| 255 | +<!-- .slide: style="font-size:80%" --> |
| 256 | + |
| 257 | +Let's say we want to go to the cinema with some friends (choose any number). |
| 258 | + |
| 259 | +How many people are going to the cinema in total? Create a variable for that. |
| 260 | + |
| 261 | +<!-- .element: class="fragment" --> |
| 262 | + |
| 263 | +A ticket to watch the movie costs 12.5€, create a variable for this as well. |
| 264 | + |
| 265 | +<!-- .element: class="fragment" --> |
| 266 | + |
| 267 | +Create a new variable that contains how much we have to pay in total. |
| 268 | + |
| 269 | +<!-- .element: class="fragment" --> |
| 270 | + |
| 271 | +Ada and Alan volunteered to pay for the tickets. Use JavaScript to compute how much each has to pay. |
| 272 | + |
| 273 | +<!-- .element: class="fragment" --> |
| 274 | + |
| 275 | +--- |
| 276 | + |
| 277 | +### Bonus |
| 278 | + |
| 279 | +1. The cinema gives a discount of 10%! Create a new variable that contains the discounted price. |
| 280 | +1. Ada and Alan have 40 EUR total. Create a new variable that is `true` if 40 EUR is enough to pay for the discounted tickets, `false` otherwise |
| 281 | + |
| 282 | +--- |
| 283 | + |
| 284 | +### Solution for bonus |
| 285 | + |
| 286 | +```js |
| 287 | +let totalPrice = 50; // result from the first task |
| 288 | +let discount = 0.1; // 10 percent discount |
| 289 | +let discountedPriceMultiplier = 1 - discount; |
| 290 | +let discountedPrice = totalPrice * discountedPriceMultiplier; |
| 291 | +let budget = 40; |
| 292 | +let canPay = budget >= discountedPrice; |
| 293 | +``` |
0 commit comments