What Are the Primitive Types in JavaScript? How To Check the Type of a Variable?

January 24, 2023

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee

Like most programming languages, JavaScript has several data types. Among them are primitive types and objects. Do you know what values are primitive in JavaScript? What are objects? And how identify the type of a variable?

JavaScript primitive types

As of now, there are seven primitive types in JavaScript data types. Other than these seven primitive types, all belong to objects. Primitive types are immutable, meaning we cannot change that value itself.

For example, string is one of the primitive types of JavaScript. We cannot change the string Hi (but in other programming languages, the string may be mutable, for example, in C it is mutable). We can only assign a variable to another string, for example:

let greeting = "Hi";
greeting = "Hello"; // assign another value, but the above 'Hi' itself does not change

The seven primitive types in JavaScript include

String

String is one of the most common primitive type. As mentioned earlier, strings themselves are immutable in JavaScript.

When we use substring() to extract a string, or use concat() to combine two strings into one, these will return another string instead of changing the original string.

Boolean

A Boolean value with two values true and false is also a primitive value of JavaScript.

Number

Unlike some languages, JavaScript does not separate integers and floating-point numbers, but uses the primitive number type. Regardless of integer or floating point, it is of type number.

In JavaScript, +Infinity, -Infinity, and NaN are all numbers type, so we use

console.log(typeof NaN); // number

will print number.

And number in JavaScript is a double-precision floating-point number, so the precision is between -(2^53 − 1) and 2^53 − 1. Outside this range, there will be precision problems, and another primitive type BigInt should be used to solve this problem.

BigInt

As mentioned above, integers and floating-point numbers in JavaScript all use the type number, which is actually only half of the story. Because JavaScript's number precision has its limitations, although most cases are good enough (2^53 - 1 will be 9007199254740991, we rarely use numbers larger than this). But sometimes you need more precision. The primitive type BigInt can be used.

BigInt allows us to choose its precision arbitrarily, which can avoid some problems that number will encounter. Like number, it can use operators such as +*-** and %, but it should be noted that BigInt cannot be used interchangeably with number type, which will cause TypeError.

Undefined

undefined is a type, which is a value itself. If a variable has not been declared, we use it first, and a reference error ReferenceError will occur in JavaScript (if the variable is declared with let and const). But if it is declared, but a value is not assigned, then because JavaScript does not know what the value of the variable is, it will print undefined.

console.log(greeting); // Uncaught ReferenceError: greeting is not defined
let greeting;

Declared but not yet assigned, will be undefined

let greeting;
console.log(greeting); // undefined

Null

null is a primitive type that is easily confused with undefined.

undefined is a variable that has not been assigned a value. For JavaScript, it does not know what the value of the variable is, so when it is to read the variable, it will be undefined. But null means that we assign a value of null to a variable.

Symbol

The last JavaScript primitive value is Symbol, which is a unique value, and it will probably be used with objects as the key of the object.

const sym = Symbol("ExplainThis");
const obj = { [sym]: "Interview Preps for Software Engineers" };
obj[sym]; // Interview Preps for Software Engineers

JavaScript objects (objects)

In addition to the seven primitive types, they are all objects in JavaScript. There is a meme in JavaScript that is

Objects, Arrays, Functions, Objects, Objects seems to be mentioned twice. Oh no, it is actually four times.

It will say that Objects is mentioned four times because in JavaScript, Array (array) and Function (function), both are objects.

How to identify the data type of a variable?

The most common way to identify the data type of a variable is through the typeof method. For example

let greeting = "hi";
console.log(typeof greeting); // 'string'

But in JavaScript, there are a few minor exceptions, one of which is null. If you use

console.log(typeof null); // object

it will print object. This is a historical bug in JavaScript, but because the cost of fixing this bug is too high, JavaScript still has this bug now. However, the data type of null should be null and not object.

typeof is not a big problem when dealing with primitive types, but as mentioned above, when we use

console.log(typeof []);

the result will be It is object, so how do we distinguish whether a variable is an object or an array?

Array.isArray() can help us.

If it is an array, it will return true; but if it is a normal object, it will return false. For example:

Array.isArray([1, 2, 3]); // true
Array.isArray({ foo: 123 }); // false

We can also use the Object.prototype.toString() method to help us distinguish between arrays, functions and general objects.

const arr = [1, 2, 3];
const fn = () => {
  return 123;
};
const obj = { foo: 123 };

console.log(Object.prototype.toString.call(arr)); // [object Array]
console.log(Object.prototype.toString.call(fn)); // [object Function]
console.log(Object.prototype.toString.call(obj)); // [object Object]
☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee