- The conditional operator assigns a value to a variable according to the result of a specified condition.
- The conditional operator has a specified syntax(way of writing/coding) instead of an assigned operator key.
- used to test the relationship between a variable’s value and another variable’s value, using the comparison operators
variablename = (condition) ? value1:value2
let time = 1800; //this is 8pm in military time
let currentSky = (time >= 1650) ? "dark":"light";
time = 700;//this is 7am military time
currentSky = (time <= 1650) ? "light":"dark";
Q : What is the result of the comparison the last line (light or dark)?
- These can be used to assign boolean values to variables or to determine the next course of action in conditional statements.
- The logical operators have symbols known as the operators that are used to perform the operations
- if the values on both sides of the operand are true, then the operation returns true. If one is false, the operation returns false. If both are false, the operantion returns false.
x = 20;
y = 30;
j = 19;
z = (x > 10) && (y < 50); // returns true
- if one or more values are true, the entire statement returns true. Only if both sides of the operand are false does the entire statement return true
x = 20;
y = 30;
z = (x > 10) || (y < 15); // returns true because the left side is true, even though the right side is false
- the not operator returns the opposite of the value it is operating on.
x = false;
y = true;
z = !x; //returns true
z = !y; //returns false