Quick Links
As with almost every programming language, JavaScript treats various types of values differently. A string like “Hello world” is very different from a number like 42. But what types are available, and why do they matter?
What Are JavaScript’s Data Types?
Every value in JavaScript has a type, one of these nine:
When working with a value, the language will often check that it has the right type. Depending on the specific case, a value with a different type may be handled automatically, or it may generate an error.

A variable that has not been assigned a value will have the undefined value. For example:
Boolean values are either true or false and are typically used in conditional logic.
Strings store text values like “hello”, “$$$”, and “A few words.” JavaScript strings are stored in UTF-16 format. In practice, this means that you can use Unicode symbols, like emojis, in your string literals:
And everything will work as expected:
Unlike many other languages, JavaScript does not distinguish between integers and floating-point numbers. 42 and 3.14 are both Number types. BigInt, as its name suggests, only covers integers, and only those whose value exceeds what Number can cope with.
BigInt values are big, very big. Try running this code in a console to fill your current page with a very big number:
Null is a special value used to represent “the absence of any value.” This might sound contradictory, but it’s useful in object-oriented programming where object references can represent relationships:
Symbol is an obscure, language-level feature that you probably won’t ever need to understand. If you curious, check outthe MDN documentation on Symbol.
Functions and Objects are values that you can create using various mechanisms, including the language’s literal syntax:
And you can create an Object using a constructor:
What Does a Type Do?
First, it’s important to note that JavaScript’s values have fixed types, but variables do not. A JavaScript variable is dynamically typed, so it adopts whatever the type of its value is:
The typeof operator returns a string that contains the type of its operand. See the following section for more info.
One of the biggest impacts that types can have is when comparing two values. JavaScript has two equality operators:==and===. These check for “loose” and “strict” equality respectively, and the difference is all about the type:
If you callcheck_equals(42, 42), you’ll see that the two values are strictly equal: they have the same value, and the same type. Butcheck_equals(42, “42”)will demonstrate that, while a number and a string can evaluate to the same value, they are still of different types, so they are only loosely equal.
The loose equality check is an example of type coercion: the automatic conversion of a value from one type to another. This is a convenience which avoids having to explicitly convert the values.
JavaScript is quite aggressive when it comes to type coercion. A type may often be silently converted when you weren’t expecting it. Most of the time, this is useful; for example:
In this case, the type ofperson.ageis “number” but JavaScript coerces it to a boolean value when evaluating the condition part of the if statement. Almost every number, whether positive or negative, will convert to “true” when coerced to a boolean. But 0 will convert to false, so coercion is a useful shortcut to avoid writingif (person.age !== 0).
Two other number values will coerce to false: -0 and NaN. NaN is a special value that stands for “Not a Number” and represents certain invalid expression results like42 / “hello”. Note thattypeof NaNreturns ‘number’! Other types also have values that coerce to false, like the empty string ("").
You may not expect a type to be coerced in a certain way. For example,[] == ““will return true because both values are coerced to boolean false. The rules for exactly which values convert to which others, in various circumstances, are lengthy and difficult to remember. To avoid having to, use===wherever possible.
How Do You Use Types in JavaScript Code?
If you want to check the type of a variable, you can use the typeof operator. For example:
typeof nullreturns ‘object’. This is considered a language design mistake, and an attempt was made to fix it, but a need for backward compatibility across the internet made such a change impractical. To check for null, use=== null.
You may be wondering where arrays fit into JavaScript’s type system. They look like a distinct type of value:
However, JavaScript considers an array to be a type of Object:
This can be inconvenient, but you can check if an object is specifically an array using the static isArray method of the Array class:
Type coercion is a particularly rich source of bugs and misunderstandings. For example:
In every case, trying to add two numbers where one is a string will result in a string value that combines the two as strings. This is because the + operator is overloaded for strings to operate as a concatenation operator rather than arithmetic addition.
However, this does not hold for multiplication:
This is the opposite case: the*operator coerces any string values into numbers, provided they look like numbers.
Type conversion is similar to type coercion, but it explicitly converts a value to a different type. A very common use of this is to convert a string into a number:
You can also construct a value of a given type using the built-in wrapper functions for each primitive (except null and undefined): Boolean, Number, BigInt, String, and Symbol.