How to Use Conditional Statements On Objects?


Conditional statements are used to control how an execution will proceed in response to various circumstances. You can take one action if a condition is true and another action if the condition is false. Conditional statements are the component of a computer program's logic, decision-making, or flow control.

The task we are going to perform in this article was using conditional statements on objects. Let’s dive into the article for getting better understanding on conditional statements.

Using if statement

The if statement is the most basic type of conditional expression. If a statement is evaluated to see whether it is true or false, the if statement will only execute if the statement returns true. In the event of a false result, the code block will be ignored.

Syntax

Following is the syntax for if statement -

if (condition) {
   // return out if condition is true
}

Example

In the following example, we are running the script by using the if statement to check whether the condition is true or false.

<!DOCTYPE html>
<html>
   <body style="background-color:#D5F5E3">
      <script>
         const amount = 1000;
         const shirt = 1500;
         if (amount <= shirt) {
            document.write("Insufficient Amount!");
         }
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of the text obtained if the condition used in the script was passed, and the text will be displayed on the webpage.

Using switch statement

A block of code is executed based on several scenarios by the switch statement. The switch statement is a component of the "Conditional" Statements in JavaScript, which are used to carry out various operations depending on certain conditions. To choose which of the numerous blocks of code will be performed, use the switch.

Syntax

Following is the syntax for switch statement -

switch(expression) {
   case a:
   break;
   case b:
   break;
   default:
}

Example

Considering the following example, where we are running the script to execute a block of code using a switch statement -

<!DOCTYPE html>
<html>
   <body style="background-color:#E8DAEF">
      <script>
      const d = new Date();
      switch (d) {
         case 1:
            document.write("Monday")
            break
         case 2:
            document.write("Tuesday")
            break
         case 3:
            document.write("Wednesday")
            break
         default:
         document.write("Waiting For The Weekend.!")
      }
      </script>
   </body>
</html>

When the above script is run, an output window will appear, displaying the information obtained from the switch statement condition that is displayed on the webpage.

Using if else statement

If a condition is true, the if statement causes a block to be executed. If the condition is false, nothing happens. However, you can use an if else statement to have a sentence run if the condition is false.

Syntax

Following is the syntax for if else statement −

if( condition ) {
   //statement
} else {
   // statement
}

Example

Execute the below code and observe how we are performing the conditional "if else" statement in the script.

<!DOCTYPE html>
<html>
   <body style="background-color:#D5F5E3">
      <script>
         let age = 20;
         if (age >18) {
            document.write('You Are Eligible For Vote.');
         } else {
            document.write('You Are Not Eligible For Vote.');
         }
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of text that was obtained by the condition used in the script and displayed on the webpage.

Updated on: 21-Apr-2023

544 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements