pluto The ? operator is like a condensed if/else statement.
If I had this code:
var num = 1;
num ? console.log("This statement is true") : console.log("This statement is false");
It would return "This statement is true" because it's the same as:
var num = 1;
if(num){
console.log("This statement is true");
}else {
console.log("This statement is false");
}
You can also chain multiple of these statements together:
var age = 22;
age < 18 ? console.log("You are a Child") : age < 65 ? console.log("You are an adult") : console.log("You are a senior");
This would return "You are an adult". This statement is the same as:
var age = 22;
if(age < 18){
console.log("You are a child");
}else {
if(age < 65){
console.log("You are an adult");
}else {
console.log("You are a senior");
}
}
As you can see, it decreased the number of lines from 10 all the way down to just 2.
Unlike if/else however, the ? statements return values that you can use in your program.
For example, I can set variables to whatever the statement returns; In this small bit of code, it checks if you're a kid or not and sets the price accordingly.
var age = 9;
var ticketPrice = age < 18 ? "$5.00" : "$10.00";
console.log(ticketPrice);
//Returns $5.00
If you need more help with it there's some topics abt it on StackOverflow and on google, here's some documentation on it https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator