The ternary Operator
a ? b : c
"If a
is true, returns b
, else it returns c
"
Let’s look at the example given:
var maior = (parseFloat(campo1) > parseFloat(campo2)? campo1 : campo2);
The <
and >
are the largest and smallest comparators you should already know. What is perhaps a novelty there is the ternary operator, delimited by ?
and :
In this case, the comparison is first resolved parseFloat(campo1) > parseFloat(campo2)
. If the result of this comparison is true, the variable maior
will receive the value of campo1
. Otherwise, you will receive the value of campo2
.
It is equivalent to the IF inline (or IIF) of some languages:
// Linguagem com operador ternário
print A > 10 ? "A é maior que dez" : "A é menor que dez"
// Linguagem com if inline
print if( A > 10, "A é maior que dez", "A é menor que dez" )
I honestly didn’t understand much of your doubt, but in this case a comparison is being made
parseFloat(campo1) > parseFloat(campo2)
and the use of?
and:
is ternary operator– Marcelo Diniz
Of course this was not a doubt but a way to win like (up Vote), ridiculous.
– noNihongo
@Eduardobentorochajunior although it seems strange, this is not against the policies of the OS, as long as it is a valid question (and really many people do not know ternary). In the beginning of the site this was a beautiful of a garimpo of points. I suggest you participate in http://meta.pt.stackoverflow.com to share and contribute what is or is not valid in Sopt
– Bacco
http://answall.com/questions/4907/diferentes-formas-de-if-e-else/
– rray
I always saw these symbols and never understood, but now with everyone’s help it’s clear, as you can see I’m no expert on javascript, so I asked this question here, and if I can win something with this, I will win. Thank you to everyone who helped me and sorry I wasn’t clear to everyone.
– Samir Braga
@Eduardobentorochajunior I understand perfectly your placement, this was one of my first questions in Meta, I am only sharing because it is an issue already solved. Also participate, the community opinion is what shapes the site! (not all, unfortunately, because initially we wanted a serious OS, not an OS with Brazilian flair, but the "leadership" did not leave).
– Bacco
@Bacco nothing personal :D, I will participate yes.
– noNihongo
The problem was I didn’t know the name was ternary.
– Samir Braga
-1 for not showing the least commitment to have sought the answer before posting the question here. This syntax is common in many languages, not Javascript. The solution was easy to find! It seemed to me a way to earn points before even reading the comments of the question.
– Andre Figueiredo
@Samirbraga the question is valid, and useful to other people. I only edited for two reasons: first to be simpler to understand and without that unnecessary comment, second to allow those who voted -1 by implication to review the vote. In any event, I voted in favour, but it had a lot of unnecessary negative votes. Pity that the staff does not have the same commitment to negativize things really with problem on the site.
– Bacco
@Andréfigueiredo but now when someone google, there is a chance to find Sopt, which is one of the goals of the site. Join http://meta.pt.stackoverflow.com to better understand the mechanics of the thing and contribute your opinion.
– Bacco
In my opinion, I understand the objectives of the site and the mechanics of the thing.. I just think I need to contribute my opinion right at the finish line, because I’m noticing a lot of point mining at Sopt with questions asked seeming to lack any effort to clear the doubt before posting here. I stand by my opinion, the question is even duplicate as mentioned in a previous comment.
– Andre Figueiredo
@Bacco In fact the way to get to the answer is different, but deep down is the same question. In fact, this is the main goal of marking as duplicate - allow the same question expressed in totally different ways to target the same resource. Reminding everyone that closing a question is not a "punishment", especially in the case of duplicates - where the question can be well asked, on topic, and the best course of action still be close.
– mgibsonbr