Lesson 3, Thursday, 2026-03-26
A variable is a container for a value. We can use it to store a number or a string that is part of a sentence.
// declare variable with name sum and number value 10
let sum = 10;
// declare variable with name sum and string value John Smith
let personName = 'John smith';Comparison Operators compare values and return true or false
===and!==→ Strict equality and inequality<and>→ Less than and greater than<=and>=→ Less than or equal to, greater than or equal to
Basic mathematical operators performs arithmetic operations:
+addition-subtraction*multiplication/division**exponentiation
- Understanding Conditional Statements
- Using Comparison Operators to test conditions
- Logical operators
- Code blocks
What comes to mind when you look at these images?

A conditional statement decides whether specific code runs based on whether a condition is true or false.
if statement specifies code to be executed if a condition is true.
if (condition) {
// run code if condition is true
}- The condition must be a boolean.
- The code in the block is executed only if the boolean is true.
- If the boolean is false, the code block is skipped.
Comparison operators are used to test if a condition is true or false so we can decide when to run certain code.
if (sum === 30) {
document.body.textContent = 'sum is equal to 30';
}if (food === "pizza") {
document.body.textContent = "My favorite food is pizza!";
}if (temperature > 20) {
document.body.textContent = "Yay, it's warm again!";
}if (canSpeakFrench) {
// same as: canSpeakFrench === true
document.body.textContent = "Bonjour!";
}Code blocks contain the code between { and }, and should be indented for better readability:
if (language === "German") {
// In VSCode, you can indent code by pressing the "Tab" key,
// or right-click and choose "Format Document"
document.body.textContent = "Guten Tag";
}All variables defined in code blocks only exist inside the block (technical term: “block scope”)
let name = "John Doe";
if (name === "John Doe") {
let greeting = "Hi dude";
}
if (name === "Mary Doyle") {
let greeting = "Hello my dear";
}
document.body.textContent = greeting; // ERROR!Correct solution:
let name = "John Doe";
let greeting;
if (name === "John Doe") {
greeting = "Hi dude";
}
if (name === "Mary Doyle") {
greeting = "Hello my dear";
}
document.body.textContent = greeting; // "Hi dude"Logical operators are helpful if you want to test multiple conditions in an if statement.
&&(AND) – All conditions must be true for the statement to execute.
||(OR) – At least one condition must be true for the statement to execute.
!(NOT) – Reverses the truth value of a condition (true becomes false and vice versa).
&& (AND)
let age = 25;
let hasLicense = true;
if (age >= 18 && hasLicense) {
console.log("You can drive!");
}|| (OR)
let hasMembership = false;
let over60 = true;
if (hasMembership || over60) {
console.log("You get a 25% discount on all in-store purchases!");
}! (NOT)
let isDaytime = false;
if (!isDaytime) {
console.log("It's nighttime!");
}What does the code below show on the HTML page?
let food = "broccoli";
if (food === "pizza") {
document.body.textContent = "yum yum";
}Solution: Nothing, since the condition in the if is false
What does it output?
let age = 42;
if (age >= 18) {
document.body.textContent = "you are allowed to drive a car";
}
if (age < 18) {
document.body.textContent = "you are not allowed to drive a car";
}Solution: First condition is true, so it outputs "you are allowed to drive a car"
Which conditions are true or false?
let age = 13;
if (age >= 18 && age <= 99) {
document.body.textContent = "Welcome! You can register";
}
let color = 'orange';
if (color === 'orange' || color === 'red') {
document.body.textContent = "The color is orange or red";
}Solution: Second condition is true, , so it outputs "The color is orange or red"
Is this code valid?
if (canSpeakFrench) document.body.textContent = "Bonjour!";Yes! if statements written without curly braces or code blocks are valid, however they are not recommend.
Code blocks help organize the code, making it easier to read and understand.
Write a code to check if the sum of two variables, x and y, is greater than or equal to 10.
Create a variable x with value of 4 and y with value of 5
Create a variable sum and assign a value of minus -1
If sum of x and y >= 10, update the variable sum with the result of x + y
Assign the value of sum to document.body.textContent.
- Try different values for
xandyto test your code. What do you observe?