JavaScript data types describe the kind of data a variable can store. Since JavaScript is a dynamically typed language, you do not need to specify the data type when creating a variable. The type is decided automatically based on the value assigned to it. Understanding data types helps you store data correctly and avoid unexpected results in your programs.
Data Types in JavaScript can be widely categorized into two types:
- Primary Data Types
- Non-Primitive Data Types
1) Primary Data Types:
Primary data types are simple and they store a single and simple value
Number
The number type is used for storing both whole numbers and decimal values.
String
Strings are used to store text. They are written inside single quotes, double quotes, or backticks. Backticks preserve line breaks and indentation in the source code, making it easy to create multiline strings without using escape characters.
Boolean
Boolean values represent true or false. They are commonly used in conditions and comparisons.
Undefined
A variable that is declared but not given a value has the type undefined.
Null
Null represents an intentional absence of a value. It means the variable is empty on purpose.
BigInt
BigInt is used for very large whole numbers that cannot be handled accurately by the Number data type.
Symbol
Symbol creates a unique value, often used to avoid naming conflicts in objects.
2) Non-Primitive Data Types
Non-primitive data types are used to store multiple values and more complex structures in JavsScript.
Object
JavsScript objects store data in key-value pairs.
Array
Arrays are used to store multiple values in a single variable. In JavaScript arrays values of different data types can be stored in the same array.
Function
Functions are treated as values in JavaScript and can be stored in variables.
Checking the Data Type
You can check the type of a value using the typeof operator.
JavaScript data types define what kind of data your program works with. Primitive types handle simple values, while non-primitive types handle collections and complex data. Knowing how and when to use each data type is essential for writing clean and reliable JavaScript code.