What are the various data types in JavaScript?
TL;DR
In JavaScript, data types can be categorized into primitive
and non-primitive
types:
Primitive data types
- Number: Represents both integers and floating-point numbers.
- String: Represents sequences of characters.
- Boolean: Represents
true
orfalse
values. - Undefined: A variable that has been declared but not assigned a value.
- Null: Represents the intentional absence of any object value.
- Symbol: A unique and immutable value used as object property keys. Read more in our deep dive on
Symbol
s - BigInt: Represents integers with arbitrary precision.
Non-primitive (Reference) data types
- Object: Used to store collections of data.
- Array: An ordered collection of data.
- Function: A callable object.
- Date: Represents dates and times.
- RegExp: Represents regular expressions.
- Map: A collection of keyed data items.
- Set: A collection of unique values.
The primitive types store a single value, while non-primitive types can store collections of data or complex entities.
Data types in JavaScript
JavaScript, like many programming languages, has a variety of data types to represent different kinds of data. The main data types in JavaScript can be divided into two categories: primitive and non-primitive (reference) types.
Primitive data types
- Number: Represents both integer and floating-point numbers. JavaScript only has one type of number.
- String: Represents sequences of characters.
Strings
can be enclosed in single quotes, double quotes, or backticks (for template literals).
- Boolean: Represents logical entities and can have two values:
true
orfalse
.
- Undefined: A variable that has been declared but not assigned a value is of type
undefined
.
- Null: Represents the intentional absence of any object value. It is a primitive value and is treated as a falsy value.
- Symbol: A unique and immutable
primitive
value, typically used as the key of an object property.
- BigInt: Used for representing integers with arbitrary precision, useful for working with very large numbers.
Non-primitive (reference) data types
- Object: It is used to store collections of data and more complex entities.
Objects
are created using curly braces{}
.
- Array: A special type of object used for storing ordered collections of data.
Arrays
are created using square brackets[]
.
- Function:
Functions
in JavaScript areobjects
. They can be defined using function declarations or expressions.
- Date: Represents dates and times. The
Date
object is used to work with dates.
- RegExp: Represents regular expressions, which are patterns used to match character combinations in strings.
- Map: A collection of keyed data items, similar to an
object
but allows keys of any type.
- Set: A collection of unique values.
Determining data types
JavaScript is a dynamically-typed language, which means variables can hold values of different data types over time. The typeof
operator can be used to determine the data type of a value or variable.
Pitfalls
Type coercion
JavaScript often performs type coercion, converting values from one type to another, which can lead to unexpected results.
In the first example, since strings can be concatenated with the +
operator, the number is converted into a string and the two strings are concatenated together. In the second example, strings cannot work with the minus operator (-
), but two numbers can be minused, so the string is first converted into a number and the result is the difference.