Data Types
Learn about Javascript Data Types, including primitive and reference types, type conversion, and type checking.
What is a JavaScript Data Type?Link to this section
A data type in JavaScript tells the computer what kind of value a variable is holding, such as a number, text, or true/false. Without data types, the computer would not know whether to add two values together like numbers or join them together like words.
This example shows a variable holding a number and using the typeof operator to check its data type.
- `let` is the variable declaration keyword
- `age` is the identifier or variable name
- `25` is the numeric value stored in the variable
- `typeof` is the operator used to find the data type
Data types are used everywhere in programming to ensure that values are processed correctly by mathematical and logical operations.
Why Do We Need JavaScript Data Types?Link to this section
Data types are necessary because different types of data require different operations and memory handling in the computer.
- Store student marks as numbers to calculate the final average
- Store customer names as text to print receipts
- Store login status as a boolean to show or hide the dashboard
- Store shopping cart items in an array to calculate the total bill
- Store user profile details in an object for easy access
- Represent missing or unassigned profile pictures using null or undefined
Treating price and quantity as numbers allows us to perform correct mathematical multiplication.
Because price and quantity are stored as number data types, JavaScript can multiply them correctly instead of treating them as text.
About JavaScript Data TypesLink to this section
JavaScript data types are broadly divided into two main categories: primitive types and reference types. Primitive types hold a single simple value, while reference types can hold collections of values or more complex entities. JavaScript is also dynamically typed, meaning a variable can hold a number at one moment and text at another.
| Category | Types Included | Mutability |
|---|---|---|
| Primitive | String, Number, Boolean, Undefined, Null, Symbol, BigInt | Immutable |
| Reference | Object, Array, Function | Mutable |
A variable can change its data type freely during the execution of a program.
Types of JavaScript Data TypesLink to this section
1. String Data TypeLink to this section
The string data type represents textual data and is enclosed in single quotes, double quotes, or backticks. It is used for names, addresses, messages, and any other text.
A basic string storing a person's name.
Using template literals with backticks to insert variables directly into text.
tip
warning
2. Number Data TypeLink to this section
The number data type represents both integer and floating-point numbers. JavaScript does not have separate types for integers and decimals.
Storing a decimal number representing money.
Calculating total price including tax using numbers.
note
3. Boolean Data TypeLink to this section
The boolean data type represents a logical entity with only two possible values: true or false. It is commonly used in conditional statements to make decisions.
A boolean representing user login state.
Evaluating a condition produces a boolean result.
tip
4. Undefined Data TypeLink to this section
A variable that has been declared but has not yet been assigned a value has the value undefined. It represents the total absence of a value.
An unassigned variable automatically holds the undefined value.
warning
5. Null Data TypeLink to this section
The null data type represents an intentional absence of any object value. It is often assigned to a variable to clear its current value or show that something does not exist.
Explicitly setting a variable to have no value.
note
6. BigInt Data TypeLink to this section
BigInt is used to represent integers that are larger than the maximum safe integer limit for standard numbers in JavaScript.
Adding an n suffix to a number turns it into a BigInt.
warning
7. Symbol Data TypeLink to this section
Symbols are unique and immutable primitive values that can be used as identifiers for object properties to prevent naming collisions.
Creating a unique symbol identifier.
note
8. Object Data TypeLink to this section
Objects are reference data types used to store collections of key-value pairs representing complex entities like users, products, or settings.
An object storing multiple related properties.
Using object properties in a practical e-commerce context.
tip
9. Array Data TypeLink to this section
Arrays are a special kind of object used to store ordered lists of values, such as lists of names, numbers, or other objects.
An array stores items in an indexed list starting at zero.
note
Type Conversion and CoercionLink to this section
JavaScript often converts data types automatically in a process called type coercion, but developers can also convert types manually using functions like Number(), String(), and Boolean().
JavaScript automatically converts the string "5" into a number during subtraction.
Manual conversion of a string to a number.
warning
Complete Coding ExampleLink to this section
This complete program demonstrates strings, numbers, booleans, arrays, and objects working together in a student profile.
This script pulls together different data types to represent a student's academic record and personal profile.
Important NotesLink to this section
- JavaScript has primitive and reference data types.
- Primitive types include String, Number, Boolean, Undefined, Null, BigInt, and Symbol.
- Reference types include Object, Array, and Function.
- Variables in JavaScript are dynamically typed and can hold any data type.
- The typeof operator is used to check the data type of a variable.
- An unassigned variable always has the value undefined.
- Null represents the intentional absence of any object value.
- Type coercion happens when JavaScript automatically converts data types during operations.
- Strings can be enclosed in single quotes, double quotes, or backticks.
- Arrays are ordered lists of values stored with zero-based indexing.
SummaryLink to this section
In this lesson, you learned about JavaScript data types, which define what kind of value a variable holds. We covered primitive types like numbers, strings, and booleans, as well as reference types like objects and arrays. Understanding data types is essential for writing error-free code and performing correct calculations.
Check Your UnderstandingLink to this section
Which operator is used to check the data type of a variable?
Practice ChallengesLink to this section
1. Store User Bio
Create variables for a user's name (string), age (number), and subscription status (boolean). Print each variable using console.log.
Expected output
John
25
trueHint: Use let to declare the variables and console.log to output each one.
2. Check Types
Create three variables holding a string, a number, and a boolean. Use typeof to print the data type of each variable.
Expected output
string
number
booleanHint: Combine typeof with console.log.
3. Shopping Receipt
Create an object representing a product with properties title, price, and inStock. Print a formatted message using these properties.
Expected output
Product: Shoes, Price: 49.99, In Stock: trueHint: Use dot notation to access object properties inside a template literal.
4. Array Items
Create an array of three favorite food items and print the second item in the array.
Expected output
PizzaHint: Remember that array indexes start at 0.
5. Type Coercion Check
Perform subtraction between a string containing a number and a regular number, then print the result and its data type.
Expected output
5
numberHint: Subtraction coerces the string into a number automatically.
Rate this content
Sign in to rate
Discussion
Sign in to join the discussion