Just Learn Code

Mastering the typeof Operator in JavaScript: Uses and Peculiarities

The Many Applications of Javascript’s typeof Operator

Perhaps the most common function in any programming language is determining the data type of a given variable. It is a necessary step in programming as it helps prevent errors and ensures appropriate processing of data.

JavaScript has a built-in operator for precisely this purpose – the typeof operator. In this article, we will cover everything you need to know about the typeof operator, its uses, and some peculiarities when working with objects and classes.

Basics of the typeof Operator

The typeof operator is a built-in operator that returns a string that represents the type of the operator’s operand. Essentially, it tells you the data type of an expression.

The syntax for the operator is straightforward: typeof operand. This operator accepts only one operand, which must be an expression, a variable, or a literal.

Data Types in JavaScript

The typeof operator will return a string based on the type of data it is evaluating. JavaScript data types include:

– String: a sequence of characters used to represent text.

– Number: is a mathematical value

– Boolean: Can be either true or false

– Undefined: Indicating that a variable is undefined

– Object: Key-value pairs, array, built-in objects, and any literal of this kind

– Function: A specific set of statements that can be defined and called separately from any other code. These are the core data types in JavaScript; however, the typeof operator can return the following additional results, which point to special JavaScript quirks:

– null: Despite being an empty or non-existent value, typeof null returns the object type, which is a known JavaScript quirk.

– Symbol: Represents a unique identifier and is usually used to support objects or classes. – BigInt: Numbers that exceed the limit of 253 – 1, such as 9999999999999999 or 9007199254740993.

Exceptions to the Rule

As previously mentioned, it’s worth noting that null has a typeof value of “object”, which seems to be a programming error. Additionally, there are some peculiarities to note with object literals.

When the operator receives an object as an operand, it will return “object”, an important nuance to remember, given that JavaScript uses objects to represent a variety of entities, including built-in objects and custom classes.

Error Cases

In addition to these peculiarities, some other errors occur when the typeof operator is used in various situations. For instance, if you declare a variable without value, its typeof value will still be “undefined”, although the value is not set.

Suppose you use typeof to evaluate a block scope variable that was never declared resulting in an error.

Working with typeof Operator

Checking for Object Type

Often, with JavaScript, developers use the typeof operator to determine if a variable is an object type to perform specific object-related operations.


let car = {

wheels: 4,


console.log(typeof car === ‘object’); //true


Checking for Function Type

Similarly, checking if the operand received is a function is also common, given the significant role functions play in programming. “`

let counter = function() {

let count = 0;

function increment() {




return increment;


console.log(typeof counter === “function”); //true


Handling Objects

Developers often use objects and their literal versions to store and manipulate related data items. For instance, take a scenario where we need to create a list of cars in an online showroom.

One way to represent the relevant data would be to create an object to store data for each vehicle. “`

let car1 = {

make: ‘Ferrari’,

model: ‘488’,

year: 2021,


let car2 = {

make: ‘Maserati’,

model: ‘GranTurismo’,

year: 2021,




Handling Classes

Using a class is quite common in JavaScript programming as it allows you to define a general structure for a data object. It can then be instantiated multiple times in different forms.

Additionally, by defining custom classes, developers can specify various properties, create custom methods, and describe behaviors specific to their application. Here’s an example of how to create a class in JavaScript.


class Car {

constructor(make, model, year) {

this.make = make;

this.model = model;

this.year = year;





In conclusion, the typeof operator is a handy feature in the JavaScript language for checking and managing data types. As we have seen in this article, it is particularly useful when working with objects and classes.

It is essential to note a few quirks of this operator, particularly when dealing with null values, and remembering that a variable is usually classed as an object. When working with classes or custom objects, it is crucial to make full use of the operator to check the types, as its output is often an essential determinant of runtime behavior.

More Fundamentals Tutorials: Additional Resources for Beginner Developers

Learning to code can be a challenging and exciting journey, and it all starts with understanding the fundamentals. The basics of coding include concepts like data types, control structures, variables, and functions.

Understanding these concepts is crucial to becoming a successful programmer. With so many resources available online, it can be challenging to know where to start.

However, this article will detail some excellent resources ideal for beginner developers to learn fundamental concepts and provide a solid foundation to develop more complex programs.


Codecademy is an online platform that provides interactive coding lessons. It is an excellent resource for learning fundamental coding concepts using a hands-on approach.

Codecademy provides courses on several programming languages, including JavaScript, Python, Ruby, and more, and their tutorials range from beginner to intermediate levels. Students get to learn by practicing what they’ve learned during each lesson, making the learning experience fun and engaging.

Each course on

Codecademy is self-paced, allowing students to learn at their own pace. Furthermore,

Codecademy’s courses are free, although a pro version is available, which provides additional material and support.


W3Schools is another platform that provides free tutorials for web development. It provides tutorials on HTML, CSS, JavaScript, SQL, PHP, and more.

What makes

W3Schools a great resource for beginners is that it provides examples with a “try it yourself” feature. In this feature, you can try out the code right in your browser and see how it behaves in real-time.

Another great feature of

W3Schools is that it provides practice exercises at the end of each tutorial. These exercises allow developers to practice the skills they’ve learned in a safe space with different problem cases.


freeCodeCamp is a non-profit organization that offers a range of tutorials aimed at teaching web development. It provides beginner-friendly lessons in HTML, CSS, and JavaScript.

The program is self-paced and provides individual challenges for students as they work through each module. One of the most impressive aspects of

freeCodeCamp is that students get to work on real-world projects as part of their training.

This hands-on approach increases the practical experience of each student. Moreover, students get to contribute to non-profit organizations’ projects through different stages of their training.


EDx is an online platform that provides free lectures, tutorials, and courses from leading universities worldwide. Harvard and MIT founded

EDx in 2012, and it offers a variety of courses covering the latest technological and scientific advancements.

The platform provides several beginner courses that cover different coding languages, including data science, web development, and more.

EDx also provides some certificates to learners upon the completion of a course, which is beneficial for your professional resume and portfolio.

Khan Academy

Khan Academy is a non-profit organization that provides free learning resources, including project-based exercises, instructional videos, and a personalized dashboard. The platform focuses mainly on mathematics, science, and coding.

Khan Academy provides an excellent introduction to JavaScript, HTML, and CSS through interactive and project-based courses. Each course is self-paced, and students can move through the modules at their own pace.


Khan Academys tutorials are interactive, allowing students to practice their coding skills throughout the courses.


The vast number of resources available online can be overwhelming for beginner developers. Besides, sorting through all of this material to find what’s best can be very challenging.

However, utilizing the resources in this article can serve as a great starting point to cover the fundamentals of coding.




EDx, and

Khan Academy all offer excellent resources to learn and grow regarding fundamental coding concepts. These platforms provide hands-on practice, interactive lessons, and project-based learning, which are all crucial for developing practical coding skills.

In this article, we’ve discussed the importance of learning fundamental coding concepts. We’ve covered several excellent resources available online for beginner developers, including




EDx, and

Khan Academy.

These platforms offer interactive and hands-on practice to help students develop practical coding skills, including control structures, data types, variables, and functions. Undoubtedly, these resources can be overwhelming, but by utilizing these platforms, beginner programmers can master the basics of coding and establish a strong foundation for their future careers in programming.

Popular Posts