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!"")