There are eight data types in JavaScript. We are going to give them a brief introduction here, and then cover each of them in detail in the next few lessons.
Seven of these data types are primitives, including numbers, BigInt
, strings, Boolean values, null
, undefined
, and symbols. They are the most basic building blocks of JavaScript.
Number and BigInt
Both the number and BigInt
are numeric values. Numbers can be defined as integers or fractional numbers.
1100; // integer
212.09; // factional number
You may also use the scientific notation:
13.14e5; // -> 314000
23.14e-5; // -> 0.0000314
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 a numeric value larger than that, you need to use a BigInt
instead. To create a BigInt
, append an n
after the integer, or use the BigInt()
function.
1let x = 1234567890987654321n;
2
3let y = BigInt(1234567890987654321);
String
String is another primitive data type in JavaScript, which can be defined in three different ways:
1// prettier-ignore
2'Hello World!'; // Single-quotes
3
4"Hello World!"; // Double-quotes
5
6`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 JavaScript console using the console.log()
method we've seen before.
This syntax leads to a problem. If the quotation marks have a special purpose in JavaScript, as they define the beginning and end of a string, then 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:
1console.log("Strings are encoded in quotes: "Hello World!"")
This is why JavaScript offers multiple ways of defining a string. In this case, you could use different quotes to avoid conflicts.
1console.log('Strings are encoded in quotes: "Hello World!"');
2
3console.log(`Strings are encoded in quotes: "Hello World!"`);
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.
1console.log(1 == 1); // -> true
2
3console.log(1 > 2); // -> false
4
5console.log(1 < 0); // -> false
6
7console.log(1 != 2); // -> true
In this example, ==
means equal, and !=
means 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 are both of their own data types, and they both indicate something does not exist.
We will discuss more about their differences later in this course.
Object and symbol
Besides the primitives, there is also a complex data type, object.
The primitives are primitives because they only store a single value, whether it is a number, a string, or something else.
Objects, on the other hand, are used to store a collection of values, allowing you to create more complex data structures. Arrays, which we've mentioned before, are actually a special type of object. It is not a specific 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, there is a typeof
operator in JavaScript that will return the data type of a given value. For example:
1console.log(typeof 123);
2console.log(typeof 1234567890987654321n);
3console.log(typeof "123");
4console.log(typeof true);
5console.log(typeof null);
6console.log(typeof undefined);
7console.log(typeof {});
1number
2bigint
3string
4boolean
5object
6undefined
7object
Notice that when you usetypeof
onnull
, JavaScript will tell younull
is an object.
1typeof null; // -> object
That's in fact a mistake in JavaScript's design. null
is not an object. It is a unique data type.