Sometimes, depending on the condition, you need to perform various actions. To do this, use the if
.
For example:
if
The if
operator (if) gets a condition, in the example above this year != 2011
. It calculates it, and if the result is true
, it executes the command.
If you need to execute more than one command, they are drawn up by a block of code in curly brackets:
3 | alert( '..и неправильно!' ); |
It is recommended to use braces always, even when the command is one. This improves the readability of the code.
Conversion to boolean type
The if (...)
operator evaluates and converts the expression in parentheses to a logical type.
In a logical context, the number 0
, the empty string ""
, null
and undefined
, and also NaN
are false
, the other values are true
.
For example, this condition will never be fulfilled:
... And this is always true:
The calculation of the condition in the if (year != 2011)
check if (year != 2011)
can be made into a separate variable:
1 | var cond = (year != 2011); |
Importance: 5
Decision
[Open task in new window]
Invalid condition, else
The optional else
block (“otherwise”) is executed if the condition is incorrect:
Several conditions, else if
Sometimes you need to check several options conditions. To do this, use the else if ...
block. For example:
In the example above, JavaScript first checks the first condition, if it is false, it goes to the second - and so on, until the last else
.
Importance: 2
Solution: tutorial / intro / ifelse_task2.html.
[Open task in new window]
Importance: 2
tutorial / intro / if_sign.html
[Open task in new window]
Importance: 3
Solution: tutorial / intro / ifelse_task.html.
Note the additional vertical indents inside the if
. They are useful for better readability of the code.
[Open task in new window]
Question mark operator '?'
Sometimes you need to assign a variable depending on the condition. For example:
Question mark operator '?'
allows you to make it shorter and easier.
It consists of three parts:
условие ? значение1 : значение2 |
The condition is checked, then if it is true - the значение1
is returned, if it is incorrect, the значение2
, for example:
access = (age > 14) ? true : false ; |
Operator '?'
runs later than most others, in particular - later comparisons, so you can not put brackets:
access = age > 14 ? true : false ; |
.. But when there are brackets, the code is better read. So it is recommended to write them.
In this case, one could do without the operator '?'
because the comparison itself already returns true/false
:
The question mark is the only operator that has as many as three arguments, while ordinary operators have one or two arguments.
Therefore, it is called the "ternary operator . "
Importance: 5
result = (a + b < 4) ? 'Мало' : 'Много' ; |
[Open task in new window]
Multiple '?'
Operators
Multiple if..else
can be replaced by a sequence of '?'
. For example:
At first it can be difficult to understand what is happening. However, after looking carefully, we notice that this is a normal if..else
!
The question mark first checks a == 1
, if true - returns a значение1
, if not - goes to check a == 2
. If this is true, it returns the значение2
, otherwise the test is a > 2
and the значение3
. Finally, if nothing is true, then the значение4
.
Alternative with if..else
:
Importance: 5
1 | var message = (login == 'Вася' ) ? 'Привет' : |
2 | (login == 'Директор' ) ? 'Здравствуйте' : |
3 | (login == '' ) ? 'Нет логина' : |
[Open task in new window]
Unconventional use of '?'
Sometimes a question mark operator is '?'
used as a replacement for if
:
It works like this: depending on the condition, either the first or the second part will be executed after the '?'
.
The result of the execution is not assigned to a variable, so it disappears (however, alert
does not return anything).
It is recommended not to use the question mark in this way.
Despite the fact that it looks shorter than if
, it is much less readable.
Here, for comparison, the same with if
:
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone