What does ? and : within an expression?

Asked

Viewed 363 times

3

I saw on a website the following code:

var campo1 = document.getElementById('valor1').value;
var campo2 = document.getElementById('valor2').value;

var maior = (parseFloat(campo1) > parseFloat(campo2) ? campo1 : campo2);
var menor = (parseFloat(campo1) < parseFloat(campo2) ? campo1 : campo2);

My question is, what does the placement of: >, <, ? and :, within these variables.

  • 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

  • 3

    Of course this was not a doubt but a way to win like (up Vote), ridiculous.

  • 2

    @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

  • http://answall.com/questions/4907/diferentes-formas-de-if-e-else/

  • 1

    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.

  • @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 nothing personal :D, I will participate yes.

  • 1

    The problem was I didn’t know the name was ternary.

  • -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.

  • 4

    @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.

  • 3

    @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.

  • 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.

  • 1

    @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.

Show 8 more comments

5 answers

6

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" )
  • 1

    Thank you, your reply is very clear.

  • 5

    The downvoters could tell you what’s wrong with this answer for people who are dumber to learn.

  • 3

    The funniest is the OS algorithm that plays questions on Twitter :) https://twitter.com/StackOverflowPT/status/448561488232742914

3

It’s a tender squeeze of a check.

The syntax is: conditional ? true : false.

Or: "se isso retornar true" ? "retorne isso" : "se não, retorne isso".

In your case, in the variable maior you are checking whether parseFloat(campo1) is greater than (>) parseFloat(campo2), if so, the value of the variable maior becomes the value of the variable campo1, otherwise becomes the variable value campo2 and so on.

It would be the same as:

if(parseFloat(campo1) > parseFloat(campo2)) {
    var maior = campo1;
} else {
    var maior = campo2;
}

But in summary form.

  • Thank you, your reply is very clear.

3

Ternary conditional operator

There is in javascript an operator that looks like an if/Else, which serves to return a value given a condition. This operator is a ternary conditional operator (meaning that it has three operands). Its form is as follows::

condicao ? parte_verdadeira : parte_falsa;

If the condition is evaluated as true, the expression returns to parte_verdadeira, otherwise returns to parte_falsa.

Example:

var x = valor > 10 ? "maior que 10" : "menor ou igual a 10";

Expression evaluated as true: condicao

The condition is any expression that can be evaluated as true or false. Note that I did not say "is true" but "can be evaluated as true". Said this way, because there are several values that are not true/false, but can be evaluated as such:

  • 0 is rated as false, and other numbers as true
  • null and Undefined are evaluated as false
  • empty string is false, any other string is true

About your doubt

The signs of < and > are only comparators being used in the ternary operator’s condition part.

  • Thank you, your reply is very clear.

2


It’s called IF TERNARY

for example:

var maior = (parseFloat(campo1) > parseFloat(campo2)? campo1 : campo2);

is the same as

maior="";
if((parseFloat(campo1) > parseFloat(campo2))
{
  maior=campo1;
}else{
  maior=campo2;
}

or after the ? it performs if the condition is true and the : is if she’s fake

more details: http://msdn.microsoft.com/pt-br/library/be21c7hw(v=vs.94). aspx

  • Great, helped me, simple and clear example.

  • I agree, your example is very clear! Thank you!

  • @Samirbraga ok, if my reply helped you please do not forget to mark it as certain.. ;)

2

Simple, this is nothing more than if and Else in a fast way When parseFloat(field1) > parseFloat(field2), if true, the variable takes field1, otherwise it will be field2.

There is still x ?? y, where if x is different from null x otherwise y.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.