JavaScript typeof operator

In JavaScript, the typeof operator can check primitive types which you learned in the previous post. Let’s find out the data types of primitive types using the typeof operator. The typeof operator shows the current data type of the variable when executed.


How to use <mark style="background-color:rgba(0, 0, 0, 0);color:#b20f0f" class="has-inline-color">typeof</mark> operator?

There are two ways to use this operator.

//Syntax #1
typeof <instance of the data type>;
//Syntax #2
typeof (<instance of the data type>);


Numbers are integers, float type and NaN that we can check using typeof operator.

//check integers
var myInt = 43;
var myFloat = 34.53;
var myNaN = NaN;
console.log(typeof myInt);
console.log(typeof myFloat);
console.log(typeof myNaN);



Strings are types that are either enclosed within double quotes or single quotes. There are special characters which are inserted into a string using escape character(\).

//string type checking
var myStr = "Notesformsc";
var myStr2 = "Educa\tion");
console.log(typeof myStr);
console.log(typeof myStr2);


The BigInt types are also number, but same as number because they comes with a suffix – n.

// Checking BigInt type
var myBI = BigInt(224);


Boolean value are only two – true or false. The result of a logical operation is also true or false.

//checking boolean values
var myBool = true;
var myBool2 = false;


A variable is not created in JavaScript until it has a value of some type. The undefined is the default value of variables not initiated.

// variable declaration
var number;
//output: undefined
// above variable is not created since not value assigned
number = 23.44; // now number is exist with a type "number"
// output : "number"

In the next section, we have discussed about complex types such null, object, function, and array.


Complex Types

The complex types are the one which acts as a data structures or are structural types such as functions. The typeof operator does retrieve the type of such types, but it is not meaningful.

Objects are created from object() constructor and function are using the function() constructor. The function constructor is part of object constructor.

Each object has a prototype property in JavaScript that link to another object creating a chain of prototypes; therefore, typeof operator is not that helpful in checking the object and instead you can use instanceof operator to verify the prototype chain and inheritance of objects.


The null is almost like undefined, but with a small difference. The assignment of null means that the variable exists, where it does not exist in the case of undefined. The null has a type of “object” .

//variable declaration
var x;
x = null; // assigned null for empty variable
//checking type
console.log(typeof x);
//output: "object"


As we mentioned earlier, object type can be constructed using Object constructor and its type is also object.

 var obj = new Object(); // constructor
obj = { name: "car", price: 1200000};
var arr = [23,45,55,66];
//checking type 
console.log(typeof arr);
console.log(typeof obj);


The function as mentioned earlier is a structural type constructed from function() constructor.

//function definition
function add()
    var A = 34 + 45;
//checking function type
console.log(typeof add);
//output : "function"

In the next post, you will start learning about various operators in JavaScript programming.


Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.