Skip to content

Latest commit

 

History

History
293 lines (180 loc) · 5.06 KB

File metadata and controls

293 lines (180 loc) · 5.06 KB

JavaScript Course - Spring 2026

Lesson 2, Tuesday, 2026-03-24


Recap

What did we talk about last lesson?


Recap: Javascript Console

F12 is the magic key

  • Alternatively, opt + cmd + C (Mac) / ctrl + shift + C (Windows)

  • Or right click on browser viewport and choose "Inspect"


Recap: data types

Number:

42

3.1415

We can also do computation, using operators:

1 + 1

10 / 5

5 ** 3

Recap: data types

String:

"Alan"

'Berlin'

Variables


Variable

A variable is a pointer to value

let price = 5;
let name = "John";

Variable


Examples:

let priceCoffee = 2;
let priceCappuccino = 3;
let customer = "John";
let greeting = "Hello";
let likesCarlo = true;
let hasKids = false;

Can you guess how we can define a variable?


How do we define a variable?

We write:

  • let
  • the variable name
  • =
  • the value we want
  • ;

Semicolon in JavaScript

Adding semicolon after statements in JavaScript is optional.

It's often considered good practice and can prevent unexpected behavior. All code in this course uses semicolons.


Variables are variable

You can change the value of a variable using the assignment operator (=):

let temperature = 24; // define a variable, initialize it to 24
temperature = 30; // assign a new value (30) to the variable
temperature = 23; // now temperature is 23

We define a variable once, then we can change it as often as needed.


try it!

Create some variables:

  • one for your full name
  • one for your age
  • one for your favorite city
  • one for whether you can speak German or not
  • and anything else you can think of :)

In the JavaScript console, enter the name of the variable. Do you see its value?

Bonus: Change the value of one of the variables that you defined.


Variables

Variables can be used wherever we can use values

let pricePerTicket = 8;
let friends = 3;
let totalPrice = friends * pricePerTicket;

Why do we use variables?

We use variables:

  • to give names (meaning) to values. "42" could mean a person’s age or shoe size.
  • for reuse and flexibility. We define the variable once with a value, and use it more than once.

Variable names

  • You can use letters, numbers, and underscore _ (spaces are not allowed!)
  • A variable name cannot start with a number

Valid variable names:

let element = 3;
let element3 = 5;

Invalid variable names:

let 2squared = 4;
let element-1 = 0;
let full name = 'Anakin Skywalker';

Giving good and descriptive names to your variables is very important!

Good variable names make the code easier to understand by other developers, and even by yourself!

Valid but not very descriptive variable names:

let a = 0;
let _12 = 0;
let asldjf = 0;
let thisisareallylongvariablename = 0;

In this course, and JavaScript in general, we use 'camelCase':

  • isStudent
  • favoriteBook
  • likesGermanFood

Variable names are case sensitive:

let name = "John";
let Name = "John";
let NAME = "John";

These are 3 different variables.


Practice

Let's say we want to go to the cinema with some friends (choose any number).

How many people are going to the cinema in total? Create a variable for that.

A ticket to watch the movie costs 12.5€, create a variable for this as well.

Create a new variable that contains how much we have to pay in total.

Ada and Alan volunteered to pay for the tickets. Use JavaScript to compute how much each has to pay.


Bonus

  1. The cinema gives a discount of 10%! Create a new variable that contains the discounted price.
  2. 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

Solution for bonus

let totalPrice = 50; // result from the first task
let discount = 0.1; // 10 percent discount
let discountedPriceMultiplier = 1 - discount;
let discountedPrice = totalPrice * discountedPriceMultiplier;
let budget = 40;
let canPay = budget >= discountedPrice;