The Conditional (Ternary) Operator Explained
How do we target what the user is entering if we have an event handler on an input field? - it will be an argument into the event handler, for example event.target.value
mutable state is typically kept in the state property of components, and only updated with setState()
<textarea> uses a value attribute, This way, a form using a <textarea> can be written very similarly to a form that uses a single-line input
Instead of using a selected attribute, use a value attribute on the root select tag
<input type=”text”>, <textarea>, and <select> all work very similarly - they all accept a value attribute that you can use to implement a controlled component.
Rewrite the following statement using a ternary statement: -
if(x===y){
console.log(true);
} else {
x===y ? console.log(true) :
an if statement, allows us to specify that a certain block of code should be executed if a certain condition is met
condition ? value if true : value if false
Finally a : colon. If your condition evaluates to false, any code after the colon is executed.
let isStudent = false;
let isSenior = true;
let price = isStudent ? 8 : isSenior ? 6 : 10
console.log(price);
// 6
It is also possible to run multiple operations within a ternary. To do this, we must separate the operations with a comma. You can also, optionally, use parenthesis to help group your code
let isStudent = true;
let price = 12;
isStudent ? (
price = 8,
alert('Please check for student ID')
) : (
alert('Enjoy the movie')
);