+ 1

conditional (Ternary) operators: (I don't get it) Please Help me?!

In JavaScript under the above title, you will see a statement: variable = (condition) ? value1 : value2 Is it that the condition is saying IF the condition is TRUE, then value1 is output as the answer, otherwise, condition MUST BE FALSE, so value2 is the output. Do I have it right? This is the only way I can make sense of it. I hope that it is correct, but if not, can someone please help me understand? I would greatly appreciate it. ~MJC

4th Oct 2017, 1:33 PM
Michael
Michael - avatar
4 Antworten
+ 4
Yes you're totally correct! alert(true? "Yas!" : "Boo~"); // Output: Yas!
4th Oct 2017, 1:56 PM
Zephyr Koo
Zephyr Koo - avatar
+ 4
You're welcome Michael! 😄 Please mark the best answer that you find useful to help others when they found this question in future.
4th Oct 2017, 3:28 PM
Zephyr Koo
Zephyr Koo - avatar
+ 2
cond ? executes if true : executes if false
4th Oct 2017, 3:12 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
@Zephyr Koo -- Oh, good. I am finally getting the hang of it. Thanks for your help and for the example. I appreciate it. 😊🤝👍
4th Oct 2017, 3:10 PM
Michael
Michael - avatar