There can be a situation where JavaScript programs involve a huge amount of data stored in the variables which need to be handled simultaneously or when the programmer needs to handle a specific variable without locating it in the complex code. In such cases, checking if the variable exists and is true becomes very handy and can save your precious time.
This article will discuss the methods to check if a variable exists and is true in JavaScript.
How to Check if Variable Exists and is True in JavaScript?
To check if a variable exists and is true in JavaScript, the following approaches can be utilized:
Go through the discussed methods one by one!
Method 1: Check if Variable Exists and is True in JavaScript Using try/catch Statements
The “try/catch” statements define a code block to run and handle the corresponding errors. This method can be used to access the declared variable in the “try” block and apply a specific condition if it is true.
The following example explains the stated concept.
Example
In the below example, the button named “Check” will be included as an input type to verify the existence of a variable and if it is true.
HTML Code
Now, the button and the heading will be accessed using the “document.querySelector()” method and stored in the variables named btnAccess and head respectively:
let head= document.querySelector("h1");
Next, assign the variable a boolean value named “true”:
JS Code
After that, an event listener named “click” will be defined which functions in such a way that when the button is clicked, the try/catch statements will execute. The “try” block will try to access the defined variable “testVar”. The catch block, on the other hand, will handle the corresponding “Reference error” in the case of failure of the try block’s execution:
let declared= true;
try {
testVar
} catch (e) {
if (e.name == "ReferenceError") {
declared= false;
}
}
Finally, a condition will be applied for checking if the variable exists and is true and it will be stored in the variable named “result”. If both the conditions are evaluated as true, the message “The variable exists and is true” will be displayed:
"The variable does not exists and is not true.";
head.innerText = result;
});
The output of the above implementation will result as follows:
Method 2: Check if Variable Exists and is True in JavaScript Using prompt() Method
The “prompt()” method asks the user for input using a dialog box. This method can be utilized to input any variable and then display the corresponding output if the variable exists and is true based on the defined functions for each of the conditions.
Go through the following demonstration.
Example
Firstly, declare a variable named testVar and assign it a boolean “true” value and an additional variable named declared as follows:
var declared;
Next, ask the user for the input variable using the “prompt()” method:
Finally, test the if condition for the specified variable in such a way that if the the entered value equals “true”, isTrue() method will be invoked, and for the other case, isFalse() method will be called which displays the added message:
declared==true
isTrue();
}
else{
declared==false
isFalse();
}
function isTrue(){
console.log("The variable exists and is true")
}
function isFalse(){
console.log("The variable does not exists and is not true.")
}
Output
We have compiled different methods for checking if a variable exists and is true in JavaScript.
Conclusion
To check if a variable exists and is true in Java script, the “try/catch” statements method can be applied to access the assigned variable, handle the exception, and specify the particular condition for the variable to meet. Also, the “prompt()” method can be implemented to ask the user to input a value and check if it is true or not. This write-up demonstrated the techniques to check if the variable exists and is true in JavaScript