What does the question mark mean in js? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What does the question mark mean in js?

I seen codes like this "7" ? "" That might not be valid, but, I seen the question mark inbetween numbers, What does it do or its job?

25th Jul 2019, 4:13 AM
Ginfio
Ginfio - avatar
2 Answers
+ 2
Ginfio are you wondering if the ternary conditional operator then yes they have worked like if else statements. Syntax: - condition ? expression1 : expression2 ā€œĀ ?Ā ā€ means ā€œifā€, and ā€œĀ :Ā ā€œ means ā€œelseā€ where theĀ conditionĀ is the value to be tested/evaluated expression1Ā can be value(s) of any type to be executedĀ if the condition is true expression2Ā can be value(s) of any type to be executedĀ if expression1 is falseĀ i.e fallback value commonly know asĀ ā€˜elseā€™ var age = 18; var canDrive = (age >= 16) ? "You're allowed to drive!" : "You should be 16 to drive!"; console.log(canDrive); // "You're allowed to drive!" Or another example can be nested conditional. var st1 = true; var st2 = true;var check = st1 ? (st2 ? "True, Yes!" : "True, False!") : 'False';console.log(check); // True, Yes! Have some šŸŽ šŸŽ šŸŽ šŸŽ
25th Jul 2019, 4:33 AM
DishaAhuja
DishaAhuja - avatar
0
I want to see this into actual if ("if") statement: var count=0; setInterval(function(){ (1?count += 1:0); pap.innerHTML = count; },100);
25th Jul 2019, 4:40 AM
Ginfio
Ginfio - avatar