JavaScript Data Types
- A data type tells JavaScript what kind of value a variable holds
- JavaScript has seven primitive data types
- Objects and arrays are non-primitive (reference) data types
- You can check a value's type using the typeof operator
- JavaScript decides data types automatically at runtime
- Working with the correct data type prevents unexpected bugs
What Is a Data Type?
A data type describes what kind of information a value represents, such as a number, a word, or a true or false answer. JavaScript uses data types to decide how a value can be used. You can add two numbers together, but adding two pieces of text works differently, so knowing the data type of a value helps you predict how your code will behave.
Primitive Data Types
Primitive data types hold a single, simple value and cannot be broken down into smaller parts.
String
Text wrapped in quotes, such as "Hello World".
Number
Any numeric value, including whole numbers and decimals.
Boolean
Only two possible values: true or false.
Undefined
A variable that has been declared but not yet given a value.
Null
Represents an intentional empty value set by the developer.
BigInt / Symbol
BigInt handles very large numbers; Symbol creates unique identifiers.
Checking a Data Type
<script>
console.log(typeof "Hello");
console.log(typeof 25);
console.log(typeof true);
console.log(typeof undefined);
</script>
Non-Primitive (Reference) Data Types
Non-primitive data types can hold collections of values or more complex structures. The two most common ones you will use constantly are objects and arrays. An object stores data as key-value pairs, similar to a real-world form with labelled fields, while an array stores an ordered list of values, similar to a numbered shopping list.
Why Data Types Matter
Choosing the right data type affects how your program behaves. Comparing a number to a string that looks like a number, for example, can produce results beginners do not expect, because JavaScript treats them differently depending on the comparison method used. Understanding data types early helps you avoid these small but confusing mistakes as your programs grow more complex.
Converting Between Data Types
JavaScript sometimes converts one data type into another automatically, which is called type coercion. You can also convert types yourself, such as turning a number into a string with String() or a string into a number with Number(). Being deliberate about conversions, rather than relying only on automatic coercion, makes your code more predictable and easier to debug.