JavaScript 101 – Lesson 07 – Decision Making and Adding Intelligence

JS101

Okay, I’m back in this series after a break! Good to have you till this mark!

Most important things in writing software are making decisions. Decisions allows us to add intelligence in our software. Basically decision means, if certain criteria happen – then we want to do something. Otherwise, we might want to do something else. We can make our programs make decisions by writing conditional statements.

We need to know about some operators before proceeding in depth of conditional statements.

 

Comparison Operators:

Comparison operator compares two values, then return true or false based upon that comparison. Available comparison operators:

==      Equal to / Equality Operator
===      Equal value and Equal type
!=      Not Equal
!==      Not equal value or Not equal type
>      Greater than
<      Less than
>=      Greater than or equal to
<=      Less than or equal to

 

Example:

var myVar = 8 < 10; //Result "true", because 8 is really less that 10
var myVar = 8 > 10; //Result "false", because 8 is never greater that 10

Similarly:

var myVar = 8 <= 10 //true
var myVar = 10 <= 10 //true

var myVar = 20 >= 20 //true
var myVar = 20 >= 30 //false

var myVar = 20 == 20 //true
var myVar = 20 == 22 //false

var myVar = 20 != 22 //true
var myVar = 22 != 22 //false

var myVar = "22" == 22 //true, checks for equality in only value. JS first convert values to same types before comparison, if it found two different value in two sides.
var myVar = "22" === 22 //false, checks for equality in both type and value
var myVar = "22" !== 22 //true, because not equal in type but value
var myVar = 25 !== 22 //true, because not equal in value but type

Comparison operators make the same sense like as we learned in our math classes. The key point is – here they results “true” or “false“.

 

Logical Operators:

Logical operator is used to produce a combined result from several comparisons.

Available logical operators:

&&      AND
||      OR
!      NOT

Comparison and Logical operators are used to test for true or false.

 

Conditional Statements:

In JavaScript we have the following conditional statements:

  • We can use if to specify a block of code to be executed, if a specified condition is true
  • We can use else to specify a block of code to be executed, if the same condition is false
  • We can use else if to specify a new condition to test, if the first condition is false
  • We can use switch to specify many alternative blocks of code to be executed

 

The if statement:

Syntax:

if (condition) {
    block of code to be executed if the condition is true
} 

Example:

if (age < 18) {
    output = "You are a minor!";
}

if (time == "18:00") {
    message = "Good Evening!";
}

 

The else Statement:

Syntax:

if (condition) {
    block of code to be executed if the condition is true
} else {
    block of code to be executed if the condition is false
}

Example:

if (age < 18) {
    output = "You are a minor!";
}
else{
    output = "You are an adult!";	
}

if (time == "18:00") {
    message = "Good Evening!";
}
else{
    message = "Hey, Good Day!";	
}

 

The else if Statement:

Syntax:

if (condition1) {
    block of code to be executed if condition1 is true
} else if (condition2) {
    block of code to be executed if the condition1 is false and condition2 is true
} else {
    block of code to be executed if the condition1 is false and condition2 is false
}

Example:

if (time == "18:00") {
    message = "Good Evening!";
}
else if(time == "06:00"){
    message = "Good Morning!";	
}
else{
    message = "Hey, Good Day!";	
}

 

The Switch Statement:

This is how it works:

  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.

The “break” keyword is used to break out of the switch block. This will stop the execution of more code and case testing inside the block.

Note: The “default” keyword specifies the code to run if there is no case match. The “default” case is optional in switch statement

Syntax:

switch(expression) {
    case n:
        code block
        break;
    case n:
        code block
        break;
    default:
        default code block
} 

Example:

switch (new Date().getDay()) {
    case 0:
        day = "Sunday";
        break;
    case 1:
        day = "Monday";
        break;
    case 2:
        day = "Tuesday";
        break;
} 

If we want to specify same block of code for several switch cases, then we can write code as below:

switch (new Date().getDay()) {
    case 1:
    case 2:
    case 3:
    default:
        text = "Looking forward to the Weekend";
        break;
    case 4:
    case 5:
       text = "Soon it is Weekend";
        break;
    case 0:
    case 6:
       text = "It is Weekend";
}

 
Some more examples:
 

Uses of logical operators

Logical AND, &&

if( age >= 18 && hairColor == "black" ){
	//Checks if both conditions are true
	alert("You are an adult guy and you have black hair.");
}
else{
	alert("You are minor!");
}

Logical OR, ||

if( age >= 18 || hairColor == "black" ){
	//Checks if any single condition is true
	alert("You are an adult guy or you have black hair.");
}
else{
	alert("Both supplied conditions were false!");
}

  
Some Example and Syntax Courtesy: http://www.w3schools.com

Leave a Comment.