![]() |
In this article, we will discuss How You Make An If Statement In JavaScript That Checks If A Variable is Equal To A Certain Word. In JavaScript, an if statement is a basic control structure that allows you to conditionally execute code based on specified conditions. A common use case is to check if the value of the variable is equal to a specific word or string. There are several approaches that can be used to Check If A Variable is Equal To A Certain Word, which are listed below:
We will explore all the above approaches along with their basic implementation with the help of examples. Approach 1: Using the Equality Operator (
|
let data = 'GeeksforGeeks' ; if (data === 'GeeksforGeeks' ) { console.log( 'The variable is equal to "GeeksforGeeks"' ); } else { console.log( 'The variable is Not equal to "GeeksforGeeks"' ); }; |
The variable is equal to "GeeksforGeeks"
In this approach, the includes() method checks if the string word contains the substring “gfg”.
Syntax:
word.includes(targetWord)
Example: In this example, we are using the above-explained approach.
const word = "GeeksforGeeks" ; const targetWord = "gfg" ; if (word.includes(targetWord)) { console.log( "The variable contains the word 'gfg'." ); } else { console.log( "The variable does not contain the word 'gfg'." ); }; |
The variable does not contain the word 'gfg'.
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |