How to Check if Variable is Declared in Javascript
In this tutorial, you will learn how to check if variable is declared in javascript. Before accessing a variable, it is extremely important to make sure that it is declared in the current context. Otherwise, you will get unforeseen errors during the compilation or runtime.
There are numerous ways to check if a variable is declared. We are going to use one of the easiest solutions which involve the usage of the try-catch
block and ternary (?
) operator. The ternary operator is also known as the conditional operator which acts similar to the if-else statement. In the try
block, we run a code that is going to be tested for errors. In the catch
block, we execute a certain piece of code after an error is thrown.
In the following example, upon click of a button, we will check if a variable is declared and display the result on the screen. Please have a look over the code example and the steps given below.
HTML & CSS
- We have 3 elements in the HTML file (
div
,button
, andh1
). Thediv
element is just a wrapper for the rest of the elements. - The
innerText
for thebutton
element is“Check”
and for theh1
element, it is“Result”
. - We have done some basic styling using CSS and added the link to our
style.css
stylesheet inside thehead
element. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div> <button>Check</button> <h1>Result</h1> </div> <script src="script.js"></script> </body> </html>
body { text-align: center; } div { display: inline-block; } button { display: inline-block; padding: 10px 20px; }
Javascript
- We have selected the
button
element andh1
element using thedocument.querySelector()
method and stored them inbtnCheck
andoutput
variables respectively. - We have a global variable
myVar
. - We have attached a
click
event listener to thebutton
element. - In the event handler function, the
isDeclared
variable is set totrue
initially. In thetry
block, we are trying to access themyVar
variable. IfmyVar
is declared, we will never enter into thecatch
block but if it does not, then we will get a Reference error. - We are using the
if
statement to verify whether the error is a Reference error or not. If yes, then we will setisDeclared
tofalse
. - We are using the ternary operator to check whether
isDeclared
istrue
orfalse
. Depending upon the result of the check, we will assign“Yes”
or“No“
to theresult
variable. - We are displaying the
result
in theh1
element using theinnerText
property.
let btnCheck = document.querySelector("button"); let output = document.querySelector("h1"); let myVar; btnCheck.addEventListener("click", () => { let isDeclared = true; try { myVar; } catch (e) { if (e.name == "ReferenceError") { isDeclared = false; } } let result = isDeclared ? "Yes" : "No"; output.innerText = result; });