Day 18 - JS types and comparisons pt.2, conditionals
by Nino · 20 things on Twos
- Making Decisions
- smart programs use booleans to make decisions on whether to run lines of code or to skip them
- if (true) {
console.log("Hello");
}
// hello
- the if statement runs code only if the boolean it's relying on is true. It's like saying, if something is true, then do this
- but what if we want to skip code? We use false instead.
- values like true are called conditions. Statements relying on conditions are called conditionals
- if statements don't decide on skipping or running the entire code
- they only make decisions about a piece of code.
- this piece of code is called a code block
- we write it between curly braces {}
- instead of using the boolean true, we can save it in a variable and use that as a condition
- const greet = true;
if (greet) {
console.log("Hello");
}
// hello
- ---
- Using Conditions
- let answer = "Picasso";
if (answer === "Picasso") {
console.log(answer + " is correct!");
}
// Picasso is correct
- let isDay = true;
if ( isDay === true) {
console.log("Lights off!");
}
// Lights off!
- let score = 51;
let pass = score > 50;
if (pass) {
console.log(pass);
}
// true
- const song = "Don't stop me now";
let replayTimes = 345;
if (replayTimes >= 300) {
console.log("Your top song this week: ");
console.log(song);
}
// Your top song this week:
Don't stop me now
- let age = 20;
let canDrive = false;
if (age > 18) {
canDrive = true;
}
console.log(canDrive);
// true
- let inboxFull = true;
const showAlert = inboxFull === true;
if (showAlert) {
console.log("Your inbox is full.");
console.log("You can't receive any more emails");
}
// Your inbox is full.
You can't receive any more emails