Table of Contents
INTRODUCTION
A lot of information floods in when dealing with technology. This information must have a nature in which it can be expressed. Data type is the way of expressing that nature. JavaScript supports 3 different categories of data. Primary, reference and special data types.
Primary supports the basic types like number, string, etc. while referential supports Object, Array, and Function. Special data type goes with undefined and null.
WHY USE TYPEOF?
JS supports dynamic variables which means that the same variable can hold different data types, like objects!. It’s important to know the data type, in order to make sense of the data.
Typeof operator in JS, returns the data type of the variable. As variables are dynamic, they don’t have a data type attached to them while they are being declared or initialized, like in other languages like C,C# etc. Typeof is used to return the variable.
Primary data type (numbers, strings, Boolean)
//primay data types
var car = "string";
var number = 2;
var flag = false;
document.write(" ", typeof car, " ");
document.write(typeof number, " ");
document.write(typeof flag, " ");
This way, you can find the types of different variables without having to run large and messy code.
REFERENTIAL DATA TYPE (OBJECT ARRAY AND FUNCTION)
Objects and functions are heterogeneous. That means that they can hold multiple data types or more precisely they can carry data of different types. Whereas, an array is homogeneous and has a contagious memory. That means all the variables will be of the same type.
FUNCTIONS
Functions can have attributes of their own. Through typeof, we can check either the function’s attribute data type, the data type the function will return or if the variable is a function or not.
function myfunc() {
var name = "codeleaks";
let num = 9;
return 1;
}
document.write(typeof myfunc());
this will give a number as a result because the return value is a number.

This is how you can check if its function or not
document.write(typeof function myfunc(){});

OBJECTS
Object’s data type, as long as it’s not null, will return “object”. To check that we can use the following code.
const Object = { name: "codeLeaks" };
document.write(typeof Object);

ARRAY
Even though arrays are homogenous but they are considered as objects in JS. A kind of an object in which typeof of an array will return as an object. In consequence, ES5 released a function isArray() to differentiate between array and object.
const array = [1, 2, 3, 4, 5];
document.write(typeof array);
document.write(Array.isArray(Object));

Note that the second value is false because an object is not an array
special data type( null and undefined)
Undefined occurs when a variable is declared and not initialized (no assigned value). Therefore, the type would be undefined.
var leaks;
document.write(typeof leaks);
Here there is no specific data type or value assigned to the variable. Therefore, the output will be undefined because it can not differentiate the container’s nature for holding any value.

You can even check the data type of undefined like this, it will give the same result.
console.log(typeof undefined);
Null’s data type is an object. This is still considered a bug because null’s purpose is to check for an empty variable and shouldn’t have the data type as an object. Therefore, typeof can’t be used with null. We can use strict (===) or loose (==) equality operators for null
var leaks;
document.write(typeof null, " ");
document.write(leaks == null, " ");
document.write(leaks === null);

The typeof of null is returned as an object, a bug. Strict equality operator checks the data type and value of the comparands. Hence, returning false because data type of null is an object while data type of variable is undefined, which are not equal.
CONCLUSION
Typeof is vastly used as its easy to use without the hassle of writing big codes or functions. Typeof is also helpful when dealing with referential data types but has its limits with null data type.It’s excellent for understanding the difference between null and undefined. Still proving to be a handy and a strong tool.