What is the if
The first thing you should understand is that the if
is not a function, is a language command (this can be seen more in What are statements and operators? and What is the difference between functions and procedures?, although the ideal is a question that asks the difference between statement and expression, but has not).
There is a confusion that some people make because of the mandatory parentheses of the conditional expression. They are not the same parentheses that are used in functions, because it is recommended to put next to the identifier in a function funcao()
and separated into a statement language standard (if (condicao)
), becomes more readable.
Another common mistake is that people think there should be a comparison there within the condition. Actually there fits any possible expression of the language, provided that the final result is a boolean value for the command if
can decide whether to execute a command below. Example Assign an expression to a variable.
Keys
The simplest way is to execute only one command so the normal is to have no keys.
The whole command structure has three parts. You have the word that defines the command (if
), the conditional expression (what is in parentheses) and then the command that must be executed conditionally.
Technically has two statements there, but the if
is a statement incomplete and needs another to perform properly, so it never ends with ;
.
It happens that in many situations it is necessary to execute several commands together depending on condition. To establish that several commands together are one thing only you use a syntax of command block. These syntax are keys. The merging of several statements become one only by the keys. Of course there will continue to be several, but for the if
only the block counts.
Note that the block has nothing to do with the if
, by chance the block can be used there, but it is not something connected to the if
. When you put the keys all those commands behave as if it were one thing and it is this thing only that will be executed if the condition is true.
Obviously keys can be used to create a block of just one statement (command), some people do so, others do not. It is a matter of taste in this case. It’s just not like when it has multiple lines belonging to the same block. See the difference:
if (false)
console.log(1);
console.log(2);
Note that only the first command after the if
It belongs to him the second no, he is isolated. It is a bad way to write because indentation tricks, but it has no relevance in JS, only keys can create a block.
Now see with keys both belong to the if
:
if (false) {
console.log(1);
console.log(2);
}
There’s even a phenomenon called dangling Else about it. What do you think happens here?
if (false)
if (true)
console.log(1);
else
console.log(2);
Sounds weird, right? The else
belongs to the if
closer to him and not apparently. With the keys there is no danger:
if (false) {
if (true) {
console.log(1);
} else {
console.log(2);
}
}
But if it indents right, it’s still legible:
if (false)
if (true)
console.log(1);
else
console.log(2);
That’s why I value indentation. Confusion only occurs in misspelled code.
Command block
Keys can be used in various situations, such as after the do
, while
, for
, the function, or even with nothing. Yes, the block can come alone only to create scope for variables. Rare to be useful in Javascript (even less if not used let
), but you could use it. What you hope will happen?
let x = 1;
{
let x = 2;
console.log(x);
}
console.log(x);
I put in the Github for future reference.
Style
I have adopted the style of when it is a line I do not use keys, but the command must come in the same line as the if
, if it’s too much and you want to put in the other line, then you have to put in keys. And if you have a clear block there will always be keys.
I see some advantage in using the keys always even for a line, I used a lot, but experience showed me that it was not necessary, I had no problems. What I find strange is to put a single line and the keys all stacked on the same line, if you will use keys then make it look like a block, even if there is only one line in that block.
You can never put more than one statement as a single thing on the same line. Each ;
is a statement
, so I guess the ;
should be mandatory, see about this.
Completion
In some ways programming is like riding Lego. You have several pieces that can fit together, use creativity to put the pieces together so that they fit. Some people have trouble programming because they don’t understand it, and they just follow cake recipes, they just copy what they saw assembled by other people. Understand what each piece of language is and you can create better toys.
When if (not only if) has only one instruction it can be used without keys.
– TiagoA
The keys
{}
are to denote a block of code. If there are multiple instructions on multiple lines they must be wrapped between keys. If there is only one instruction or more in a single row then you can do this variationif(condicao) instrucao1; instrucao2;
– GeekSilva
@Geeksilva This information is not correct:
if(condicao) instrucao1; instrucao2;
will always perform theinstrucao2
. Only theinstrucao1
is covered byif
.– LS_ᴅᴇᴠ