You may need to check if a variable is defined or initialized in JavaScript before making a reference to it. Otherwise, your script will result in errors. Learn to get this done below.
Check If A Variable Is Defined Or Initialized In JavaScript
typeof
The typeof operator in javascript can tell you the type of its operand’s value. Its syntax is simple:
typeof operand
The returned value is a string representing the type of the primitive or object you provide. Its possible returns include “string”, “number”, “boolean”, “bigint”, “object”, “function”, “symbol”, and “undefined”.
This list is exhaustive and defined by the ECMAScript standards. No spec-compliant browsers produce values not in this list, except for old Internet Explorer versions.
This means you can use the typeof to check whether a variable’s value is undefined or defined by using a simple if statement:
if (typeof yourVar === 'undefined') {
// your code here
}
The snippet above will run the code in the if block if the ‘yourVar’ variable’s value is undefined.
It is important to note that in JavaScript, this doesn’t necessarily mean the variable hasn’t been declared. Newly declared variables that have not been assigned any value will be automatically assigned the primitive value “undefined” as well.
Example:
let s1 = "LearnShareIT";
let s2;
if (typeof s1 == 'undefined') {
console.log("s1's value is undefined");
}
if (typeof s2 == 'undefined') {
console.log("s2's value is undefined");
}
if (typeof s3 == 'undefined') {
console.log("s3's value is undefined");
}
Output:
s2's value is undefined
s3's value is undefined
In this example, we use the typeof operator to check the type of the values of three variables.
The first one (s1) has been declared and given a string value. Therefore, typeof will return “string”, not “undefined”, and the if statement won’t execute the console.log() method in its code block.
The s2 variable has been declared, but we don’t assign any value to it, while the s3 variable hasn’t been declared at all. The typeof operator will return “undefined” in both cases, prompting the if statement to run the code in its main block.
try-catch With ReferenceError
JavaScript will print an error when your script makes a reference to a variable that hasn’t been declared. It will also create a ReferenceError object to represent this error. You can use the try-catch statements to catch it.
try {
let s1 = "LearnShareIT";
} catch (e) {
if (e instanceof ReferenceError) {
console.log("s1 hasn't been declared");
}
}
try {
let s2;
s2;
} catch (e) {
if (e instanceof ReferenceError) {
console.log("s2 hasn't been declared");
}
}
try {
s3;
} catch (e) {
if (e instanceof ReferenceError) {
console.log("s3 hasn't been declared");
}
}
Output:
s3 hasn't been declared
Since both s1 and s2 have been declared, their try statements won’t throw any error when they are referenced to. On the other hand, the reference to s3 will raise a ReferenceError object.
Scope Of Variables
Like other functions, methods, and operators, typeof and try-catch can only access values that have been made “visible” to it.
This means your variable should be in the scope where typeof or try-catch is invoked. Otherwise, they will return “undefined” regardless of whether the variable has been declared or assigned a value.
Example:
function sum(a, b) {
let x = a + b;
console.log(x);
}
console.log(typeof x);
Output:
undefined
The variable x in this example has been declared inside the function sum, giving you the function scope. You aren’t able to access it outside of that function.
When you have multiple variables of the same in different scopes, typeof will return the type of the variable it can access:
function sum(a, b) {
let x = a + b;
console.log(x);
console.log(typeof x);
}
sum(3, 4);
let x = "LearnShareIT";
console.log(typeof x);
Output:
7
number
string
We have two variables x in this script: one in the function sum() and one in the main block. The former has the function scope, while the latter has the global scope.
When you check the type of x with typeof in sum(), it will return “number”, while doing so outside of the function will return “string”.
Summary
To check if a variable is defined or initialized in JavaScript, you can use the typeof operator or try-catch statement. Make sure they are called in the same scope of the variable you want to test. If you want to know whether a variable is null, have a look at this guide.
Maybe you are interested:
- Check if Variable is of Function Type using JavaScript
- Check if a Variable is of type Error in JavaScript
- Check if a variable is a string using javascript
- How To Concatenate A String With A Variable In JavaScript?
- Check if Variable Doesn’t Equal Multiple Values in JavaScript

My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Job: Developer
Name of the university: HUST
Major: IT
Programming Languages: Java, C#, C, Javascript, R, Typescript, ReactJs, Laravel, SQL, Python