What are the JavaScript Data types?

An overview of the 8 most important Data Types in JavaScript, why they are important and how to use them.

9 months ago   •   4 min read

By Codaisseur
Table of contents

If you just started programming, you probably already heard about JavaScript  data types, maybe you are already familiar with strings, numbers and even objects or arrays. And that’s because data types are a very important concept in programming, the computer needs to know exactly what we mean, so it will be able to operate on the variables.

In this article, I will show you some of the most common data types, and we will discuss all of them in more detail. There are 8 basic data types in JavaScript: String, Number, BigInt, Boolean, Null, Undefined, Objects and Symbols. They are divided into two categories: primitive and non-primitive, only the Objects belong to the non-primitive category.

Strings in JavaScript

The string data type represents text and, in JavaScript, a string must always be surrounded by quotes, you can use double quotes, single quotes or backticks, the backticks adds extra functionality to our string, and we can use it to display a variables' values.

As you see in the code snippet above, we can concatenate the strings or combine them inside the backticks quotes. Also, when displaying a variable, we always have to use ${} and put it inside the curly brackets.

Number and BigInt in JavaScript

The number data type is used to represent positive or negative numbers with or without decimal place, and unlike some other programming languages, JavaScript does not define other types of number, like integers, short, long, etc. There are many operations for numbers: multiplication *, division /, addition +, subtraction -, and so on.

Besides regular numbers, there are some “special numeric values” which also belong to this data type: Infinity, -Infinity and NaN.

The BigInt type was recently added to JavaScript to represent integers of arbitrary length, because the number type cannot represent integer values larger than (253-1) or less than -(253-1) for negatives and sometimes we need really big numbers.

Booleans in JavaScript

The boolean type has only two values: true and false. It is commonly used to store values like yes (true) or no (false), on (true) or off (false). It's also very used in comparisons inside if/else statements, like you can see in the following example.

Undefined and Null in JavaScript

When we declare a variable, but don't assign any value to it, we'll get the value of undefined. So, basically, a variable without a value, has the value undefined.

The null data type is supposed to mean "nothing", it’s a special value which represents “nothing”, “empty” or “value unknown”. You can also use null to empty an object, for example.

The types undefined and null are equal in value, but different in type, undefined is type undefined while null in an empty object.

The Objects and Symbols type

All other types are called “primitive” because their values can contain only a single thing, objects are used to store collections of data and more complex entities.

An object contains properties, defined as a key-value pair. A property key (name) is always a string, but the value can be any data type, like strings, numbers, booleans, or complex data types like arrays, functions and other objects.

There are many other kinds of objects in JavaScript: Array, Date, Error and more. Here you can see an example of an array of objects

Symbols were introduced to JavaScript in ECMAScript 2015, they are often used to identify object properties, mostly to avoid name clashes between properties, since no symbol is equal to another or to add properties that the user cannot overwrite.

I hope this post was clarifying and helped you learn a lot about JavaScript data types, to dive deeper in the subject you can always consult the JavaScript documentation. Got any questions, remarks or additions for us, please email to support@codaisseur.com, and we will get back to you.

Interested in learning more about Javascript?

Spread the word

Keep reading