JavaScript Simple Conditional Execution.
1 min readFeb 24, 2021
Conditional Execution is used when you have 2 different results for an specific case condition. For example if you want to print a result based on a comparison:
cosnt number = -4;
if(number > 0){ //Conditional execution
console.log(`Number ${number} is positive.`)
}else {
console.log(`Number ${number} is negative.`)
}
The JavaScript workflow is changed by the conditional statements if and else. For other hand, you can use while and do loops to change the flow over loop situations like:
let number = 0;
while(number <= 4){ //Conditional execution
console.log(`Count: ${number}`);
number++;
}
//output
Count: 0;
Count: 1;
Count: 2;
Count: 3;
Count: 4;
Also you can break the workflow for a loop using the break keyword, In the previous example we can add an if condition to break the loop when the number change to 3:
let number = 0;
while(number <= 4){ //Conditional execution
console.log(`Count: ${number}`);
number++;
if(number === 3){
break;
}
}
//output
Count: 0;
Count: 1;
Count: 2;
Count: 3;
If you are interesting you can check out this Eloquent JavaScript.