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
872 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 theif elsestatement

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.

     
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.

     
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.

   

Pick The Color

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!