JavaScript Roadmap — Day 4: Data Types — String, Number, and Boolean
Data Types — String, Number, and Boolean
The three most important primitive types in JavaScript — and why understanding them prevents 90% of beginner bugs.
Every program works with data — names, numbers, scores, prices. JavaScript needs to know what kind of data it is dealing with so it can handle it correctly. That is what data types are for.
In this lesson we cover the three most common primitive data types: String, Number, and Boolean. Master these and you have the foundation of every JavaScript program you will ever write.
1. String — Text Data
A String is any piece of text. You create a string by wrapping text in quotes — single quotes, double quotes, or backticks. Backticks (template literals) are the most powerful because they let you embed variables directly inside the text.
💡 Important: Strings are immutable — once created, the original string never changes. String methods always return a new string.
2. Number — Numeric Data
JavaScript has only one type for all numbers — both whole numbers and decimals. Unlike other languages, there is no separate int or float. One type handles everything.
isNaN(value) — never use === NaN because NaN is not equal to itself.(0.1 + 0.2).toFixed(2) to round your result to 2 decimal places.Infinity — not an error. This is very different from Java or Python. You can check using isFinite(value) to know if a number is a valid finite result.3. Boolean — True or False
A Boolean has only two possible values: true or false. Booleans are the language of decisions — every if statement and every comparison in JavaScript produces a boolean result.
4. The typeof Operator
Use typeof to check the data type of any value. It returns a string describing the type. Essential for debugging.
Quick Reference
| Type | Example | typeof | Used for |
|---|---|---|---|
| String | "Waheed" |
"string" |
Names, messages, text |
| Number | 22, 3.14 |
"number" |
Age, price, score |
| Boolean | true, false |
"boolean" |
Decisions, conditions |
Try It Yourself
Edit the code and click Run Code. Try changing the values and see how typeof responds.
Practice Tasks
Task 1 — Know Your Types
Create variables for your name (String), your age (Number), and whether you are a student (Boolean). Log each one with its type using typeof.
Task 2 — Template Literals
Using the variables from Task 1, write a sentence using a template literal: "My name is [name], I am [age] years old and I am a student: [true/false]."
Mini Challenge — Fix the Bug
Run console.log(0.1 + 0.2) in the playground. Then fix the output to show exactly 0.30 using .toFixed(2). This is a real bug every JavaScript developer encounters.