How Do You Make An If Statement In JavaScript That Checks If A Variable is Equal To A Certain Word?


The task we are going to perform in this article is about how do you make an if statement in JavaScript that checks if a variable is equal to a certain word.

What is an if...else statement

When the condition in the if statement is true, a block of code will be executed. This is a form of conditional statement. The else block will be invoked if the condition is false.

In if statements, truthy and falsy values are transformed to true or false.

Syntax

Following is the syntax for if else statement

if (condition is true) {
   // code is executed
} else {
   // code is executed
}

Let’s look into the following examples for getting more idea on making an if statement in JavaScript that checks if a variable is equal to a certain word.

Example

In the following example, we are using an if statement and running a condition to check whether it matches the given word or not.

<!DOCTYPE html>
<html>
   <body>
      <script>
         var str ="Welcome To The TutorialsPoint."
         if(str.match("hy")) {
            alert('Match Found');
         } else {
            alert('No Match Found');
         }
      </script>
   </body>
</html>

When the script gets executed, it will generate an output checking the given condition and verifying it with the given text and displaying an alert. In our case, it will display "No Match Found."

Example

Consider the following example, where we are passing a condition and comparing it with the original statement and getting the output.

<!DOCTYPE html>
<html>
   <body>
      <script>
         const age = 25;
         if (age >= 22) {
            document.write("Welcome");
         } else {
            document.write("TutorialsPoint");
         }
      </script>
   </body>
</html>

On running the above script, the output window will pop up, displaying the text "welcome" obtained from the if statement that matches the given condition.

Example

Execute the below code and observe how the if statement was executed in JavaScript that checks if a variable is equal to a certain word.

<!DOCTYPE html>
<html>
   <body>
      <h3>Pick The Color</h3>
      <label>
         <input type="radio" name="color" value="Green" onclick="pickcolor(this.value);"> Green
      </label>
      <label>
         <input type="radio" name="color" value="Black" onclick="pickcolor(this.value);"> Black
      </label>
      <script>
         function pickcolor(myColor) {
            if (myColor == "Green") {
               alert("Save Environment.!");
            }
            else {
               alert("Not Selected Green");
            }
         }
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of a list of radio buttons with two values: one is green, and the other is black. When the user clicks on the green color, the alert "Save the environment!" appears.

Updated on: 20-Apr-2023

345 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements