Menu Close

Articles

JavaScript: decision

One of the most important feature of a computer language is the logical deduction, inference or decision making. Without a decisions statement a program is linear. Programs can have a complex execution path similar to a river bifurcation. Therefore a decision statement is sometimes called flow control statement.

What is a decision?

A decision is a block statement controlled by a conditional expression or value. Decision is used to execute different actions based on result of one condition. A conditional expression is also known as logical expression.

A condition can be evaluated to a Boolean values: true or false. Depending on the result of conditional expressions the program can execute one logical path from two possible. The expression can be as simple as a variable of type Boolean.

Logical expressions

To create a logical expression we use variables, constant literals, comparison operators and logical operators. The result of the expressions can be assigned to a variables or can be used by decision statements to control the program workflow.

Numbers can be compared using “relation operators” also known as “comparison operators”. Characters in a string also can be compared and ordered in “alphabetic order”. 

SymbolDescription
==Equality
!=Inequality
===Identity
!==Non-identity
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Notes:

  1. Bang symbol “!” is used for “negation”. Can be used in front of an expression to negate, therefore expression !(a==b) is equivalent with (a!=b).
  2. You can combine multiple sub-expressions into larger expressions using two logical operators:
    • “||” is used for logical operation: “OR”
    • “&&” is used for logical operation: “AND”
Alert: Never use single symbol “=” into a logical expression. You can use “==” or “!=” to compare two variables. You can also use “===” or “!==”.  Symbol “=” is not a logical operator.

Decision Statements

In JavaScript there are several decision statements:

  • if: specify a block of code to be executed, if a condition is true;
  • else: specify a block of code to be executed, if a condition is false;
  • else if specify a new condition to test, when the earlier condition is false;
  • switch specify many alternative logical routes for program depending on a value.

IF/ELSE Example :

Notes:

  • Condition (x < y)  is enclosed in round brackets.
  • In this case the message will be “good” because indeed  x is less than y.

Switch Example:

In this example for each day number from 0 to 6 we get the day name in English.

Notes: 

  • We use break statement for each case to exit the switch and do not check all cases;
  • Default branch will execute if no other case is executed;

Fall-through Example:

In this example we detect a workday from a weekend day. The cases are executed top down if a break do not exist. In all cases 1,2,3,4,5 we have workday in cases 0 or 7 we have WeekDay. This technique is called fall through.

Note: The switch statement is not the same in all languages. For example Go language execute one single case by default and break automatically. The fallthrough is a keyword that must be used for this effect in Go.

Next article: Repetition