Syntax: variablename = (condition) ? value1:value2
Examples:
let time = 1800; //this is 6pm in military time
let currentSky = time >= 1650 ? "dark" : "light";
The currentSky variable will evaluate to “dark” if the condition is true, or “light” if the condition is false.
Q : What is the result of the comparison (light or dark)?
In this example:
time = 700; //this is 7am military time
currentSky = time <= 1650 ? "light" : "dark";
JavaScript’s logical operators are used to determine whether an entire statement/condition is true or false depending on the operation and values involved.
These can be used to assign boolean values to variables or to determine the next course of action in conditional statements.
Operator: &&
Examples:
x = 20;
y = 30;
j = 19;
z = x > 10 && y < 50; //returns true
Operator: ||
Examples:
x = 20;
y = 30;
z = x > 10 || y < 15; //returns true because the left side is true, even though the right side is false
Operator: !
Examples:
x = false;
y = true;
z = !x; //returns true
z = !y; //returns false
See the Pen Exercise 2.5 by LSU DDEM (@lsuddem) on CodePen.