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.
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.
The following is the syntax of theif elsestatement
if (condition is true) { // code is executed } else { // code is executed }
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.
In the example below, we use if statement and run the condition to check if it matches the given word.
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".
Consider the following example where we pass a condition and compare it with the original statement and get the output.
When running the above script, an output window will pop up showing the text "welcome" obtained from the if statement matching the given condition.
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.
Pick The Color
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!