JavaScript

What is the typeof Operator in JavaScript?

The typeof is an operator present in most programming languages and is used to check the datatype of an operand (opernad: the variable which is operated on).

In this write-up, we will learn all about the typeof operator, what it is and how to use it; but first, we need to understand what data types are.

What are data types:

Data types are a classification of data that defines how data can be stored and manipulated. Each programming language has built-in data types which might be different from other programming languages.

Here’s a list of the six most basic data types in JavaScript:

  • Number: As the name implies, it consists of numbers
  • Boolean: Booleans can have only two different values; true or false.
  • String: Strings are a collection of alphanumeric characters.
  • Undefined: Empty or undeclared variable.
  • Object: Bundle/Collection of Data

The typeof is not a function, but rather it is an operator. In programming languages, functions and operators are different; they may behave similarly but are synthetically and semantically different.

How to use typeof operator:

A variable is passed to the typeof operator as a parameter and returns the variable’s datatype.

Syntax

typeof (var)

typeofvar

Both of the syntaxes mentioned above are correct. The operand can be written with or without parentheses.

Note: We will use the browser console to demonstrate examples performed in this post. To open up the browser console:

  • Use the F12 key in Chrome and other chromium-based browsers.
  • Use CTRL + SHIFT + K keyboard shortcut keys for Mozilla.
  • Use Option + ⌘ + C keyboard shortcut keys in Safari (if the developer menu does not appear, open Preferences by pressing ⌘ + , and in the Advanced tab, check “Show Develop menu in menu bar”).

Examples:

Now we will look at a few examples of typeof operators. The typeof operator is present in many programming languages, but we will use the JavaScript syntax for these examples.

In the code given below, we have declared a variable named age and assigned it a value of 10. Then we used the typeof operator to determine the datatype of age. The typeof operator was put inside console.log() to output the return value of the typeof operator on the console.

var age = 10;
console.log(typeof(age));

If we assign a new value to age, i.e., ten instead of 10, then the output changes to:

var age = 'ten';
console.log(typeof(age));

Now, if we do not assign any value to age and use the typeof operator on it; then the operator will return the following value:

var age;

console.log(typeof(age));

We can use the same method to determine other data types as well like boolean, object and symbol, etc.:

var x = true;

console.log(typeof(x));

As you have already seen in the above examples, the typeof operator can be combined with other functions and methods such as conolse.log. It can also be combined with conditionals, loops, etc.

In the example given below, the typeof operator is used with conditional statements:

var age = 5;
if (typeof(age) == 'number')
{
console.log('The provided number is in the form of digits.')  
}
else
{
console.log('The provided number is not in the form of digits.')
}

Now, if we change the age from 5 to five, then the output changes to:

Conclusion

The typeof operator returns the datatype of the operand (the variable which is passed as the parameter to the operator). It is very helpful in programming languages such as JavaScript as it has dynamic data types. Dynamic data type means that the same variable can store different data types within a single program.

In this write-up, we have learned what data types are and an operator in programming languages. Moreover, we have also learned about the typeof operator and how to use it using JavaScript syntax.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.