• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Check if Variable Exists

Using JavaScript to Check if Variable Exists

July 25, 2022 Leave a Comment

One of the easiest ways we can use JavaScript to check if a variable exists is with exception handling using a try-catch statement.

try{
  console.log(some-variable);
} catch(err) {
  console.log("variable doesn't exist");
}

#Output:
variable doesn't exist

This way will make sure that if you try to check if a variable exists that has not been initialized, you can handle the error that will be returned so that it does not break your program.


Another easy way that we can use JavaScript to check if a variable exists is by using the JavaScript typeof Operator in an if conditional statement.

if ( typeof someVariable !== "undefined" ){
  //The variable DOES exist
}

In the code above, someVariable is the variable we want to check to see if it exists. typeof someVariable will return “undefined” if the variable does not exist, and therefore the conditional statement will be false.

Now let’s show some examples of this code in use:

var variable1 = {name:'tom', age:25};
var variable2 = [1,2,3,4];
var variable3;
var variable5 = undefined;
var variable6 = {};

if ( typeof variable1 !== "undefined" ){
  console.log("variable exist");
} else {
  console.log("variable doesn't exist");
}

if ( typeof variable2 !== "undefined" ){
  console.log("variable exist");
} else {
  console.log("variable doesn't exist");
}

if ( typeof variable3 !== "undefined" ){
  console.log("variable exist");
} else {
  console.log("variable doesn't exist");
}

if ( typeof variable4 !== "undefined" ){
  console.log("variable exist");
} else {
  console.log("variable doesn't exist");
}

if ( typeof variable5 !== "undefined" ){
  console.log("variable exist");
} else {
  console.log("variable doesn't exist");
}

if ( typeof variable6 !== "undefined" ){
  console.log("variable exist");
} else {
  console.log("variable doesn't exist");
}

#Output:
variable exist
variable exist
variable doesn't exist
variable doesn't exist
variable doesn't exist
variable exist

Hopefully this article has been useful for you to learn how to use JavaScript to check if a variable exists.

Other Articles You'll Also Like:

  • 1.  innerHTML vs outerHTML
  • 2.  JavaScript If Not – Check if a Condition is False
  • 3.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 4.  Using JavaScript to Remove Quotes From a String
  • 5.  JavaScript Print Array – Printing Elements of Array to Console
  • 6.  Get a List of Prime Numbers Using JavaScript
  • 7.  JavaScript if else Shorthand Statement
  • 8.  JavaScript Trig – How to Use Trigonometric Functions in Javascript
  • 9.  JavaScript Check If Number is a Whole Number
  • 10.  JavaScript Check if Attribute Exists in HTML Element

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy