Conditional Logic Loops and Operators 1
Conditional Logic Loops and Operators 1
SYNTAX
• Most of the time, OR || is used in an if statement to test if any of the given conditions is true.
EXAMPLE
let hour = 12;
let isWeekend = true;
if (hour < 10 || hour > 18 || isWeekend) {
alert( 'The office is closed.' ); // it is the weekend
}
&& (AND)
• let user;
• alert(user ?? "Anonymous");
// Anonymous (user not defined)
EX2
let user = "John";
alert(user ?? "Anonymous"); // John (user defined)
CONTINUED…
EXAMPLE
let firstName = null;
let lastName = null;
let nickName = "Supercoder"; // shows the first defined value:
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); //
Supercoder
NOTE: Using OR|| OPERATOR STILL RETURNS THE SAME OUTPUT
OR returns the first truthy value while
nullish coalescing operator ?? Returns first defined value
Avoid using ?? With && or || unless you introduce brackets
LOOPS: WHILE AND FOR
• Loops are a way to repeat the same code multiple times.