Home > Web Front-end > JS Tutorial > How to create an if statement in JavaScript to check if a variable is equal to a certain word?

How to create an if statement in JavaScript to check if a variable is equal to a certain word?

PHPz
Release: 2023-08-29 09:13:08
forward
931 people have browsed it

如何在 JavaScript 中创建 if 语句来检查变量是否等于某个单词?

The task we are going to perform in this article is how to create an if statement in JavaScript to check if a variable is equal to a certain word.

What is if...else statement

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

In the if statement, true and false values ​​are converted to true or false.

grammar

The following is the syntax of the if else statement

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

Let’s take a look at the following example to understand how to create an if statement in JavaScript to check if a variable is equal to a certain word.

Example

In the example below, we use if statement and run the condition to check if it matches the given word.

<!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>
Copy after login

When the script executes, it will generate an output, check the given conditions and validate with the given text and display an alert. In our case it will display "No Match Found".

Example

Consider the following example where we pass a condition and compare it with the original statement and get the output.

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

When running the above script, an output window will pop up showing the text "welcome" obtained from the if statement matching the given condition.

Example

Execute the following code and observe how an if statement is executed in JavaScript to check 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>
Copy after login

When the script executes, it will generate an output containing a list of radio buttons with two values: one is green and the other is black. When the user clicks green, an alert appears that says "Save the environment!" Appear.

The above is the detailed content of How to create an if statement in JavaScript to check if a variable is equal to a certain word?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template