• 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 is an Object

Using JavaScript to Check if Variable is an Object

June 13, 2022 Leave a Comment

One of the easiest ways we can use JavaScript to check if a variable is an object is by using the JavaScript typeof Operator in an if conditional statement.

if ( typeof someVariable == "object" ){
  //The variable IS an object
}

In the code above, someVariable is the variable that we want to check to see if it is an object. typeof someVariable will return “object” if the variable is an object, and therefore the conditional statement will be true.

We can put this code in a function to make checking if a variable is an object very simple.

Here is the function:

function isObject(variable){
  if ( typeof variable == "object" ){
    return true;
  } else {
    return false;  
  }
};

Our function simply returns true if the variable you enter is an object, and false if not.

Now let’s show some examples of this function is use:

function isObject(variable){
  if ( typeof variable == "object" ){
    return true;
  } else {
    return false;  
  }
};

var variable1 = {name:'tom', age:25};
var variable2 = [1,2,3,4];
var variable3 = "1234";
var variable4 = ["Red","Yellow","Green","Blue"];
var variable5 = function isObject(variable){
  if ( typeof variable == "object" ){
    return true;
  } else {
    return false;  
  }
};
var variable6 = {};

console.log(isObject(variable1));
console.log(isObject(variable2));
console.log(isObject(variable3));
console.log(isObject(variable4));
console.log(isObject(variable5));
console.log(isObject(variable6));

#Output:
true
true
false
true
false
true

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

Other Articles You'll Also Like:

  • 1.  JavaScript atanh – Find Hyperbolic Arctangent of Number
  • 2.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 3.  Using JavaScript to Check if Variable is a Number
  • 4.  Using JavaScript to Return String
  • 5.  Using JavaScript to Set Visibility
  • 6.  Changing the Background Image of a div in JavaScript
  • 7.  Using JavaScript to Return Two Values
  • 8.  Using JavaScript to Check if String Contains Only Letters
  • 9.  JavaScript Capitalize First Letter of Every Word
  • 10.  Set the Height of a Div Using JavaScript

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