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
trueorfalsevalues. - 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
Symbols. - BigInt: Represents integers with arbitrary precision.
The remaining ECMAScript language type is Object. Arrays, functions, dates, regular expressions, maps, and sets are all kinds of objects rather than additional language types. Functions are callable objects and receive the special typeof result "function".
Data types in JavaScript
JavaScript values belong to primitive language types or the Object language type.
Primitive data types
- Number: Represents both integer and floating-point numbers. JavaScript only has one type of number.
let age = 25;let price = 99.99;console.log(price); // 99.99
- String: Represents sequences of characters.
Stringscan be enclosed in single quotes, double quotes, or backticks (for template literals).
let myName = 'John Doe';let greeting = 'Hello, world!';let message = `Welcome, ${myName}!`;console.log(message); // "Welcome, John Doe!"
- Boolean: Represents logical entities and can have two values:
trueorfalse.
let isActive = true;let isOver18 = false;console.log(isOver18); // false
- Undefined: A variable that has been declared but not assigned a value is of type
undefined.
let user;console.log(user); // undefined
- Null: Represents the intentional absence of any object value. It is a primitive value and is treated as a falsy value.
let user = null;console.log(user); // nullif (!user) {console.log('user is a falsy value');}
- Symbol: A unique and immutable
primitivevalue, typically used as the key of an object property.
let sym1 = Symbol();let sym2 = Symbol('description');console.log(sym1); // Symbol()console.log(sym2); // Symbol(description)
- BigInt: Used for representing integers with arbitrary precision, useful for working with very large numbers.
let bigNumber = BigInt(9007199254740991);let anotherBigNumber = 1234567890123456789012345678901234567890n;console.log(bigNumber); // 9007199254740991nconsole.log(anotherBigNumber); // 1234567890123456789012345678901234567890n
Object type and common object kinds
The entries below are common kinds of objects, not separate ECMAScript language types.
- Ordinary object: Often used to store keyed data. Object literals are created using curly braces
{}.
let person = {name: 'Alice',age: 30,};console.log(person); // {name: "Alice", age: 30}
- Array: A special type of object used for storing ordered collections of data.
Arraysare created using square brackets[].
let numbers = [1, 2, 3, 4, 5];console.log(numbers);
- Function:
Functionsin JavaScript areobjects. They can be defined using function declarations or expressions.
function greet() {console.log('Hello!');}let add = function (a, b) {return a + b;};greet(); // "Hello!"console.log(add(2, 3)); // 5
- Date: Represents dates and times. The
Dateobject is used to work with dates.
let today = new Date().toLocaleTimeString();console.log(today);
- RegExp: Represents regular expressions, which are patterns used to match character combinations in strings.
let pattern = /abc/;let str = '123abc456';console.log(pattern.test(str)); // true
- Map: A collection of keyed data items, similar to an
objectbut allows keys of any type.
let map = new Map();map.set('key1', 'value1');console.log(map);
- Set: A collection of unique values.
let set = new Set();set.add(1);set.add(2);console.log(set); // Set(2) {1, 2}
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.
console.log(typeof 42); // "number"console.log(typeof 'hello'); // "string"console.log(typeof true); // "boolean"console.log(typeof undefined); // "undefined"console.log(typeof null); // "object" (this is a historical bug in JavaScript)console.log(typeof Symbol()); // "symbol"console.log(typeof BigInt(123)); // "bigint"console.log(typeof {}); // "object"console.log(typeof []); // "object"console.log(typeof function () {}); // "function"
Pitfalls
Type coercion
JavaScript often performs type coercion, converting values from one type to another, which can lead to unexpected results.
let result = '5' + 2;console.log(result, typeof result); // "52 string" (string concatenation)let difference = '5' - 2;console.log(difference, typeof difference); // 3 "number" (numeric subtraction)
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 subtracted, so the string is first converted into a number and the result is the difference.
Further reading
- JavaScript data types and data structures - MDN
- JavaScript guide: Data types
- Data types - javascript.info
- JavaScript data types