Coding for All
Lesson 12: The OR Operator
The OR Operator Combine conditions with ||.
1const likesCats = true;
2const likesDogs = false;
3if (likesCats || likesDogs) {
4 console.log("You like animals!");
5}
Terminal
$ node main.js
You like animals!
Every line, explained
const likesCats = true;
A boolean is a value that can only be true or false (both lowercase in JavaScript).
const likesDogs = false;
A boolean is a value that can only be true or false (both lowercase in JavaScript).
if (likesCats || likesDogs) {
if checks the condition inside its round brackets. If the condition is true, JavaScript runs the code between { and }; if it is false, it skips that code completely. || means OR: the whole condition is true if at least one side is true, and true || false is true. (The | character is a vertical bar.)
console.log("You like animals!");
console.log(...) prints whatever is inside the round brackets to the terminal, then moves to a new line. console is the terminal itself, log is the tool that writes to it, joined with a dot.
}
This } closes the if block.