# 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

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);

//Output
"number"
"number"
"number"

## String

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);
//Output
"string"
"string"

## BigInt

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

// Checking BigInt type
var myBI = BigInt(224);
console.log(myBI);
//Output
"bigint"

## Boolean

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;
console.log(myBool);
console.log(myBool2);
//Output
"boolean"
"boolean"

## undefined

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;
console.log(number);
//output: undefined
// above variable is not created since not value assigned
number = 23.44; // now number is exist with a type "number"
console.log(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.

## null

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"

## 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);
//output:
"object"
"object"

## Function

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

//function definition
//output : "function"