Introduction
Welcome to the JavaScript documentation! This lab will give you an introduction to conditionals.
This tutorial is from open-source community. Access the source code
Welcome to the JavaScript documentation! This lab will give you an introduction to conditionals.
Open the Terminal/SSH and type
node
to start practicing coding.
Conditionals are code structures used to test if an expression returns true or not. A very common form of conditionals is the if...else
statement. For example:
let iceCream = "chocolate";
if (iceCream === "chocolate") {
console.log("Yay, I love chocolate ice cream!");
} else {
console.log("Awwww, but chocolate is my favorite…");
}
The expression inside the if ()
is the test. This uses the strict equality operator (as described above) to compare the variable iceCream
with the string chocolate
to see if the two are equal. If this comparison returns true
, the first block of code runs. If the comparison is not true, the second block of code—after the else
statement—runs instead.
Congratulations! You have completed the Conditionals lab. You can practice more labs in LabEx to improve your skills.