JavaScript If Instructions

JavaScript If and If Else Instructions

IF Instruction

The most basic instruction, we can find these instructions in almost every programming language (perhaps with slightly different syntax). This instruction allows us to execute a block of instruction if the condition of the conditions are met.
The syntax of this instruction is as follows:

if (requirements are met) {
list of instructions or blocks of instruction
Some important notes about these instructions:
  • the condition must be located between two curly braces
  • we can put several conditions by using the operators AND or OR  (&& or ||)
For example:
if ((condition 1) && (condition2))
if ((condition 1) || (condition2))
If there is only one instruction in the block of instruction, we do not need to use parentheses, as the following example:
if (x == 2)
document.write ("The value of x is 2");
If .. Else InstructionsIf instruction is basic instruction that allows us to examine whether a condition is fulfilled or not. In fact we want more than one state requirements to run the program, because of that, we can use the instructions IF ... Else ....

Complete syntax of this instruction is:
if (requirement1 is met) {
list of instructions or blocks of instruction
else {
list of instructions or blocks of instruction other


Post a Comment