Skip to content

Basic data types in JavaScript

June 11, 2025

5 min read

Just like every other programming language, JavaScript has its data types. Even though we don’t have to define data types (it does it for us), we should understand how they work. In practice, this means we can create a variable with a numeric value and then set it as an object, and the JavaScript interpreter will automatically change the type.

The fact that we don’t have to specify types in JavaScript might give the impression that it’s simpler, but it’s rather the opposite. Code correctness checking can be more complicated, and it can happen that we overlook a change in data type. The basic data types of JavaScript are:

Number

JavaScript uses the number type to represent both integers and floating-point numbers. There are many operations for numbers, such as multiplication *, division /, addition +, subtraction -, and so on. Any numeric literal can be preceded by a minus sign (-) to make the number negative.

let num = 100;
let price = 12.5;
let discount = 0.05;
let negativeNumber = -10;

Note that JavaScript automatically converts a floating-point number to an integer if the number appears to be a whole number. The reason is that JavaScript always wants to use less memory, because a floating-point value uses twice as much memory as an integer value. For example:

let price = 100.00; // Interpreted as an integer 100

This type has a certain range, and if you work with large numbers, you can get the type’s range using Number.MIN_VALUE and Number.MAX_VALUE.

console.log(Number.MIN_VALUE); // 5e-324
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308

JavaScript represents numbers using the 64-bit floating-point format defined by the IEEE 754 standard,1 which means it can represent numbers as large as ±1.7976931348623157 × 10308 and as small as ±5 × 10−324.

In addition to regular numbers, there are so-called “special numeric values” that also belong to this data type: Infinity, -Infinity, and NaN.

When the result of a numeric operation is greater than the largest representable number (overflow), the result is the special infinite value Infinity. Similarly, when the absolute value of a negative value becomes greater than the absolute value of the largest representable negative number, the result is negative infinity, -Infinity.

console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(Number.MAX_VALUE + Number.MAX_VALUE); // Infinity
console.log(-Number.MIN_VALUE - Number.MIN_VALUE); // -Infinity

NaN

NaN represents a computational error and is a special numeric value that indicates an invalid number. It is the result of an incorrect or undefined mathematical operation (such as 0/0). If NaN appears anywhere in a mathematical expression, it propagates to the entire result (meaning that once you get NaN as a result, any further mathematical operation with NaN will return NaN).

console.log( "not a number" / 2 ); // NaN, such  division is erroneous
console.log(NaN / 2); // NaN, division by NaN is also erroneous
console.log(NaN == NaN); // false, NaN is not equal to itself
console.log('a' / 2); // NaN, division by a string is also erroneous

JavaScript supports numeric literals in several formats, and I mention the two most common ones.

Integer Literals

JavaScript numeric literals include integer literals in various bases as well as floating-point literals in base-10. In addition to base-10 integer literals, JavaScript recognizes hexadecimal (base-16) values.

// Decimal integer literal
0 
12
1000000

// Hexadecimal integer literals
0xFF
0x3b24

// Binary integer literal
0b1010

// Octal integer literal
0o12

Floating-Point Literals

Floating-point literals can have a decimal point; they use traditional syntax for real numbers. They can also be represented using exponential notation: a real number followed by the letter e (or E), followed by an optional plus or minus sign, followed by an integer exponent.

3.1415926
.123456789
3.1E+12
.1e-23

Rounding Errors

David Flanagan aptly notes that when working with numbers in JavaScript, you’ll encounter situations where not all numbers are always equal, even though it may appear so at first glance.

When you’re working with real numbers in JavaScript, the representation of the number will often be an approximation of the actual number.

JavaScript numbers have great precision and can approximate a given number very closely. But the fact that this number cannot be expressed exactly can lead to problems. Look at the following code:

It’s good to know that this problem is not specific to JavaScript: it affects any programming language that uses binary floating-point numbers.

let a = 0.1 + 0.2; // 0.30000000000000004
let b = 0.3; // 0.3
console.log(a === b); // false, because 0.1 + 0.2 is not exactly equal to 0.3
let c = 0.1 + 0.2 - 0.3; // 5.551115123125783e-17
console.log(c); // 5.551115123125783e-17, a very small number close to zero

In the last example, you might expect c to equal 0, but it actually equals a very small number close to zero: approximately 5.551115123125783e-17.

Dates and Time

JavaScript dates and times provide built-in functions for working with dates, times, and time zones. They allow developers to easily create, manipulate, format, and compare dates and times in their applications.

These functions provide support for various time zones and allow developers to work with dates and times in different regions and cultures.

let timestamp = Date.now(); // The current time as a timestamp (a number)
let now = new Date(); // Current date and time as a Date object
let ms = now.getTime(); // Convert to a milliseconds
let iso = now.toISOString(); // Convert to a string in standard format

BigInt

In JavaScript, the number type cannot safely represent integer values larger than (2^53-1) (which is 9007199254740991) or smaller than -(2^53-1) for negative values. Therefore, the BigInt type was recently added to the language, which represents integers of arbitrary length.

A BigInt value is created by appending n to the end of an integer:

let bigInt = 1234567890123456789012345678901234567890n;

Since BigInt numbers are rarely needed, I won’t describe this type in much detail. You can learn more about it on the MDN website.

Boolean

See all posts