Conditional operators | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Conditional operators

So in a Syntax like this one var age = 42; var isAdult = (age < 18) ? "Too young": "Old enough"; document.write(isAdult); the first option has the value "false" and the second the "true" one. I tried to change the places of "Too young" and "Old enough" and it shown me "Too young". Is my opinion right?

24th Dec 2017, 6:42 PM
Leo Glekler
Leo Glekler - avatar
7 Answers
0
Assignments x=y are almost always true. Your example in human format: If spec is 0 then return loser else return winner If (spec==0) then (return “Loser”) else (return “Winner”) Same as: (spec ==0)?”Loser”:”Winner” If you used spec = 0 then that almost always return true (!=0)
24th Dec 2017, 7:25 PM
H Chiang
+ 2
in (..) you have a statement, then '?' ask the statement: "Is it true or false?", if its true the code will execute the first part of the condition(before colon), otherwise it will execute second part
24th Dec 2017, 7:02 PM
Botol
Botol - avatar
+ 1
spec == 0, change) it checks if spec == 0, and if its true is will be equal to "winner"
24th Dec 2017, 7:13 PM
Botol
Botol - avatar
+ 1
the '?' asks "Is spec equal to zero?" and the answer is true so the code will execute first part(spec will be equal to "winner")
24th Dec 2017, 7:17 PM
Botol
Botol - avatar
0
Well, I'm angry on my brother and I wrote this cute programm for him. var Chris = "Chris is a "; var spec = 0; // if it's zero, then Chris* is a loser var is =(spec = 0)?"Winner":"Loser"; document.write(Chris + is); As (spec = 0) is true, and Chris is a loser, I must put the "Loser" option on the second place. This means following: (true)?"false":"true" = true (false)?"false":"true" = false It's also logical to do that. It's beginning with a zero and not with a one. *name changed :D
24th Dec 2017, 7:12 PM
Leo Glekler
Leo Glekler - avatar
0
What?!, O.o Why?
24th Dec 2017, 7:14 PM
Leo Glekler
Leo Glekler - avatar
0
My mind is too stupid for this 😂😂 Thanks for explanation :)
24th Dec 2017, 7:20 PM
Leo Glekler
Leo Glekler - avatar