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

In JavaScript, you can use an if statement to check if a variable equals a specific word using the equality operator (==) or strict equality operator (===).

What is an if...else Statement

An if statement executes a block of code when a condition is true. The else block runs if the condition is false. This is a fundamental conditional statement in JavaScript.

Syntax

if (condition) {
   // code executes if condition is true
} else {
   // code executes if condition is false
}

Basic String Comparison

The most straightforward way to check if a variable equals a word is using the equality operator:

<!DOCTYPE html>
<html>
<body>
   <script>
      var word = "JavaScript";
      
      if (word === "JavaScript") {
         alert("Match Found!");
      } else {
         alert("No Match Found");
      }
   </script>
</body>
</html>

Using String Methods

You can also use string methods like match() for more flexible comparisons:

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

Practical Example with User Input

Here's an interactive example that checks user selection against a specific word:

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

Case-Sensitive vs Case-Insensitive Comparison

JavaScript string comparison is case-sensitive by default. For case-insensitive comparison, use toLowerCase():

<!DOCTYPE html>
<html>
<body>
   <script>
      var userInput = "HELLO";
      var targetWord = "hello";
      
      // Case-insensitive comparison
      if (userInput.toLowerCase() === targetWord.toLowerCase()) {
         alert("Words match (case-insensitive)!");
      } else {
         alert("Words don't match");
      }
   </script>
</body>
</html>

Key Points

  • Use === for strict equality (recommended)
  • Use == for loose equality (performs type conversion)
  • String comparisons are case-sensitive by default
  • Use toLowerCase() or toUpperCase() for case-insensitive matching

Conclusion

Creating if statements to check if variables equal specific words is straightforward using equality operators. Use strict equality (===) for precise matching and consider case sensitivity based on your requirements.

Updated on: 2026-03-15T23:19:01+05:30

567 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements