Data Types
In real life, we work with different kinds of information. A name is text, a price is a number, and a yes or no decision is a boolean.
JavaScript also needs to understand what kind of data it is working with. This is where data types come into the picture.
What Is a Data Type?
A data type tells JavaScript what kind of value is stored in a variable. Different data types behave differently when used in calculations, comparisons, or conditions.
Understanding data types helps you write correct and predictable code.
Real-World Example
Imagine filling out an online form:
- Your name is text
- Your age is a number
- Whether you agree to terms is true or false
If JavaScript treats all of these the same way, mistakes will happen. Data types help JavaScript handle each value correctly.
Common JavaScript Data Types
At a beginner level, these are the most important data types you should know:
- String
- Number
- Boolean
- Undefined
- Null
String
A string represents text. It is written inside quotes.
let userName = "Dataplexa";
Strings are commonly used for names, messages, labels, and text content.
Number
The number data type is used for numeric values. This includes whole numbers and decimal values.
let price = 199.99;
let quantity = 3;
Numbers are used in calculations such as totals, discounts, and scores.
Boolean
A boolean represents a true or false value.
let isLoggedIn = true;
Booleans are commonly used in conditions and decision making.
Undefined
A variable is undefined when it is declared but has not been assigned any value.
let userEmail;
JavaScript automatically assigns undefined
when no value is provided.
Null
null means an empty or intentionally missing value.
let selectedItem = null;
Developers use null when they want to clear a value on purpose.
Checking the Data Type
JavaScript provides the typeof operator
to check the type of a value.
typeof "Hello";
typeof 100;
typeof true;
This is useful for debugging and understanding how data is being handled.
Thumb Rules
- Use strings for text data
- Use numbers for calculations
- Use booleans for conditions
undefinedmeans no value assignednullmeans value intentionally empty
More on Data Types Later
In this lesson, we focused only on the basic data types that you will use frequently as a beginner.
More advanced concepts are intentionally kept for later lessons.
- Complex data types like arrays and objects are covered in Lesson 11 (Arrays) and Lesson 13 (Objects)
- Type conversion and advanced behavior will appear in later lessons
What Comes Next?
Now that you understand different data types, the next step is learning how to work with them using operators.
In the next lesson, we will explore JavaScript operators.