Conditions in JavaScript
Operator |
Usage |
description |
Logical AND (&&) |
exp1 && exp2 |
Only if both are TRUE |
Logical OR (||) |
exp1 || exp2 |
at least one of them is TRUE |
Logical NOT (!) |
!exp |
is not TRUE |
Precendence |
ex1 && ex2 || ex3 && ex4 |
AND checked before OR |
Ternary Operator
a short way to write a condition
var beverage = (age >= 21) ? "Beer" : "Juice";
var resultHolder = (condition) ? TRUE value : FALSE value;
- Definition of variable beverage
- Checking a condition (with a different varible in this case)
- ?
- What to return if TRUE
- :
- What to return if FALSE
- ;
the new variable will contain one of the strings
With multiple conditions:
var resultHolder = (condition) ? value1
:(condition) ? value2
:(condition) ? value3
:value1;
back to main page