Table of Contents

Exploring Different Data Types in JavaScript

Code Playground is only enabled on larger screen sizes.

Data is at the core of programming. What a program does is essentially take some input data, process it, and return some output data.

There are eight different types of data that JavaScript can process.

Here, we'll give them a brief introduction, and then cover each of them in detail in the next few lessons.

Number and BigInt

Both number and BigInt are numeric values.

Numbers can be defined as integers or fractional numbers.

javascript
100; // integer
12.09; // factional number

You may also use the scientific notation:

javascript
3.14e5; // -> 314000
3.14e-5; // -> 0.0000314

numbers in JavaScript

JavaScript uses a fixed number of bits to represent a number, meaning it can only work with numbers up to 15 digits.

If you need numeric values larger than that, use BigInt instead. To create a BigInt, append an n after the integer, or use the BigInt() constructor.

javascript
let x = 1234567890987654321n;
 
let y = BigInt(1234567890987654321);

String

String is another primitive data type in JavaScript, which can be defined in three different ways:

javascript
// prettier-ignore
'Hello World!'; // Single-quotes
 
"Hello World!"; // Double-quotes
 
`Hello World!`; // Backticks

It doesn't matter if you use single quotes, double quotes, or backticks, as long as the opening and closing quotes match.

The strings can be printed to the console using the console.log() method, as we've demonstrated before.

javascript
console.log("Hello World!");

This syntax leads to a problem.

If the quotation marks have a special purpose in JavaScript, what should we do if we need a quotation mark to be a part of the string?

For example, if you want to print the sentence Strings are defined with quotes: "Hello World!", the following code will return an error:

javascript
console.log("Strings are encoded in quotes: "Hello World!"");

Syntax error

This is why JavaScript offers multiple ways of defining a string. In this case, you could use different quotes to avoid conflicts.

javascript
console.log('Strings are encoded in quotes: "Hello World!"');
 
console.log(`Strings are encoded in quotes: "Hello World!"`);

mismatch quotes

Boolean value

The only two Boolean values are true and false. true indicates something is correct, and false indicates something is wrong.

One of the most common ways to produce Boolean values is through comparison operations.

For instance, you can compare two numbers, and the result of that comparison will be a Boolean value.

javascript
1 == 1; // -> true
1 > 2; // -> false
1 < 0; // -> false
1 != 2; // -> true

compare numbers

In this example, == indicates equal, and != indicates not equal. Other similar operators include >= (greater than or equal to) and <= (less than or equal to).

null and undefined

null and undefined are two special values in JavaScript.

They both indicate something does not exist, but they are both of their own data types.

We will discuss more about their differences later in this chapter.

Object and symbol

All of the data types we discussed above are primitives, meaning they only store a single value, whether it is a number, a string, or something else.

Besides the primitives, there is also object, which allows you to store a collection of values.

Arrays, which we've mentioned before, are actually a special type of object. It is not a dedicated data type in JavaScript.

Symbols are used to create identifiers for the object. We will discuss symbols in more detail after we discuss objects.

The typeof operator

Lastly, the typeof operator in JavaScript that will return the data type of a given value. For example:

javascript
typeof 123; // -> number
typeof 1234567890987654321n; // -> bigint
typeof "123"; // -> string
typeof true; // -> boolean
typeof null; // -> object
typeof undefined; // -> undefined
typeof {}; // -> object

Notice that typeof null gives object.

javascript
typeof null; // -> object

That's in fact a mistake in JavaScript's design.

null is not an object. It is a unique data type, null.