What does it mean? and: within an expression? [duplicate]

2

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 those variables.

    
asked by anonymous 25.03.2014 / 21:34

5 answers

1

This is called IF TERNÁRIO

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;
}

that is after% w / o% it executes if the condition is true and ? is if it is false

More details: link

    
25.03.2014 / 21:38
6

The ternary operator

 a ? b : c
  

"If a is true, returns b , otherwise returns c "

Let's look at the given example:

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

The < and > are the largest and smallest comparators you should ever know. What is perhaps new here is the ternary operator , delimited by ? and :

In this case, the parseFloat(campo1) > parseFloat(campo2) comparison is first resolved. If the result of this comparison is true, the variable maior will receive the value of campo1 . Otherwise, it will get the value of campo2 .

It is equivalent to the inline IF (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" )
    
25.03.2014 / 21:38
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 shape is as follows:

condicao ? parte_verdadeira : parte_falsa;
  

If the condition evaluates to true, the expression returns parte_verdadeira , otherwise returns 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 "it's true" but "can be assessed as true". That said, because there are several values that are not true / false, but can be evaluated as such:

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

About your question

Signals of < and > are only comparators being used in the part of the condition of the ternary operator.

    
25.03.2014 / 21:42
2

It is a ternary expression 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 if parseFloat(campo1) is greater than ( > ) parseFloat(campo2) , if it is, the value of the maior variable becomes the value of the campo1 variable %, otherwise the value of the variable campo2 and so on.

It would be the same as:

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

But in a nutshell.

    
25.03.2014 / 21:39
1

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

Is there still x ?? y, where x is different from null it will result in x sin y.

    
25.03.2014 / 21:38