How to check the type of object or variable in JavaScript?

Checking the type of object or variable is significant in any programming language, especially in JavaScript.

In this article, we will walk you through how to do it using several methods to accomplish this task effectively.

So, let’s get started and explore several ways to check the type of an object or variable in JavaScript.

What is data type in JavaScript?

There are seven primitive types that are not objects in in JavaScript. It includes the following:

📌BigInt

📌Boolean

📌null

📌Number

📌String

📌Symbol

📌Undefined

These primitive types are the basic building blocks in JavaScript. On the other hand, anything that is not one of these primitive types is considered an object.

This includes arrays and functions, which are treated as objects because they are collections of data or executable code.

An object in JavaScript is like a container that can hold multiple values or functions, organized as key-value pairs.

Objects allow us to represent more complex data structures and perform various operations on them.

Solutions on how to check the type of object in JavaScript

The following are the solutions which you can use to check the type of object in JavaScript.

Solution 1: Use the typeof operator

The typeof operator gives you a string that tells you the type of the value stored in the operand.

To check the type of a variable or object, you can use the “typeof” operator. It takes a variable and gives you its type as a string.

Syntax

typeof operand

or

typeof variable_name 

For example:

console.log(typeof 18);
// output: "number"

console.log(typeof "Itsourcecode");
// output: "string"

console.log(typeof false);
// output: "boolean"

console.log(typeof undeclaredVariable);
// output: "undefined"

Output:

number
string
boolean
undefined

Here’s another example code:

let sample = "Itsourcecode";
console.log(typeof sample); // Output: string

Output:

string

Examples of typeof operator in JavaScript

typeof 18 === "number";
typeof 1.5 === "number";
typeof Math.LN2 === "number";
typeof Infinity === "number";
typeof NaN === "number"; // despite if it is Not a Number
typeof Number("1") === "number"; // The Number tries to convert different types of data into numbers.
typeof Number("Itsourcecode") === "number"; // This includes values that cannot be directly converted into a number.

typeof 18n === "bigint";

typeof true === "boolean";
typeof false === "boolean";
typeof Boolean(0) === "boolean"; 
typeof !!1 === "boolean"; //Using the logical NOT (!) operator twice is equivalent to using the Boolean() function.

typeof "" === "string";
typeof "Itsourcecode" === "string";
typeof `Itsourcecode` === "string";
typeof "3" === "string"; // always remember that any number within a string is still typeof string

typeof Symbol() === "symbol";
typeof Symbol("itsourcecode") === "symbol";
typeof Symbol.iterator === "symbol";

let x;
typeof x;  //'undefined'
typeof y;  //'undefined'
typeof undefined;  //'undefined'

typeof null;  //'object';

typeof { x: 1 } === "object";
typeof [1, 2, 4, 4, 5] === "object";

typeof new Date() === "object";
typeof /regex/ === "object";

typeof new Boolean(true) === "object";
typeof new Number(1) === "object";
typeof new String("xyz") === "object";

typeof function () {} === "function";
typeof class C {} === "function";
typeof Math.sin === "function";
typeof (() => {});  //'function';
typeof Math.sqrt;  //'function'

Solution 2: Use instanceof operator

In JavaScript, you can use the “instanceof” operator to check if an object is created from a specific constructor. It will give you a boolean (true or false) result.

Syntax:

object_name instanceof object_constructor 

Here’s an example:

let sampleArray = [1, 2, 3, 4, 5];
console.log(sampleArray instanceof Array); // Output: true

Output:

true

Solution 3: Use Object.prototype.toString.call()

Another way to check the type of an object in JavaScript is by using the Object.prototype.toString() method to find out the class of an object.

It provides a clear and definitive way to determine the class of an object.

Here’s an example:

let sampleDate = new Date();
console.log(Object.prototype.toString.call(sampleDate)); // Output: [object Date]

Output:

[object Date]

Solution 4: Use constructor property

The constructor property is a property of an object that points to the constructor function responsible for creating the object. In simple terms, it reveals the type of the object.

For instance, when you create an array using the Array constructor, the constructor property of that array will refer to the Array constructor function.

You can use the constructor property of an object to determine its type.

let sampleArray = [1, 2, 3, 4, 5];
console.log(sampleArray.constructor === Array); // Output: true

Output:

true

Conclusion

In conclusion, this article discussed several methods to effectively check the type of object or variable in JavaScript.

By understanding the data types in JavaScript and recognizing the distinction between primitive types and objects, developers can effectively perform type checks and make informed decisions in their code.

The article explored four different solutions for type checking.

The typeof operator can be used to obtain the type of a variable or object by returning a string representation of its type.

The instanceof operator allows checking if an object is created from a specific constructor, providing a boolean result.

Another approach is using the Object.prototype.toString.call() method, which gives a string representation of an object’s type.

Additionally, the constructor property can be utilized to determine the type of an object by comparing it with the corresponding constructor function.

By employing these methods, developers can accurately identify the types of objects and variables in JavaScript, enabling them to write more robust and error-free code.

We are hoping that this article provides you with enough information that helps you understand the JavaScript check type of object.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment