Skip main navigation

Analyzing and modifying data types: Working out the type of a variable

In this article, we’ll explore JavaScript methods that will help us deal with common problems.
Someone working on a laptop.
We have seen the primitive data types.

There are some built-in JavaScript methods that will help us deal with common problems related to primitives. Built-in methods are pieces of logic that can be used without having to write JavaScript logic yourself.

There are many of these built-in methods, and the ones you will be meeting in this chapter are just the first few you will encounter.

We’ve seen one built-in method already: console.log().

Working out the type of a variable

Especially with null and undefined, it can be hard to determine what kind of data type you are dealing with. Let’s have a look at typeof. This returns the type of the variable. You can check the type of a variable by entering typeof, then either a space followed by the variable in question, or the variable in question in brackets:

testVariable = 1; 
variableTypeTest1 = typeof testVariable;
variableTypeTest2 = typeof(testVariable);

As you might assume, both methods will output number. Brackets aren’t required because technically, typeof is an operator, not a method, unlike console.log.

But, sometimes you may find that using brackets makes your code easier to read. Here you can see it in action:

let str = "Hello"; 
let nr = 7;
let bigNr = 12345678901234n;
let bool = true;
let sym = Symbol("unique");
let undef = undefined;
let unknown = null;

console.log("str", typeof str);
console.log("nr", typeof nr);
console.log("bigNr", typeof bigNr);
console.log("bool", typeof bool);
console.log("sym", typeof sym);
console.log("undef", typeof undef);
console.log("unknown", typeof unknown);

Here, in the same console.log() print command, we are printing the name of each variable (as a string, declared with double quotes), then its type (using typeof). This will produce the following output:

str string 
nr number
bigNr bigint
bool boolean
sym symbol
undef undefined
unknown object

There is an odd one out, and that is the null type.

In the output you can see that typeof null returns object, while in fact, null truly is a primitive and not an object. This is a bug that has been there since forever and now cannot be removed due to backward compatibility problems.

Don’t worry about this bug, as it won’t affect our programs—just be aware of it, since it will go nowhere anytime soon, and it has the potential to break applications.

© Packt
This article is from the free online

Introduction to JavaScript

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now