Data Types
Discover the different kinds of data you can work with in programming—text, numbers, true/false values, and more.
Just like in real life, programs work with different kinds of information. A name is different from a number, which is different from a yes/no answer. Data types help the computer understand what kind of information it's dealing with.
Why Data Types Matter
Imagine you're filling out a form. Some fields need text (your name), some need numbers (your age), and some are checkboxes (yes/no). Computers work the same way—they need to know what type of data they're handling to process it correctly.
Real-World Connection
You can't add your name to your age—that doesn't make sense! Similarly, computers need to know data types so they can perform the right operations. You can add two numbers, but "adding" two names means combining them into one string.
The Basic Data Types
1. Strings (Text)
A string is any text wrapped in quotes. It could be a single letter, a word, a sentence, or even an empty string.
let firstName = "Alice";
let message = "Hello, World!";
let empty = "";
let number = "42"; // This is a string, not a number! Watch out: "42" in quotes is a string, not a number!
The quotes make all the difference.
2. Numbers
Numbers are exactly what they sound like—they can be whole numbers (integers) or decimals (floating-point numbers).
let age = 25; // Integer
let price = 19.99; // Decimal
let negative = -10; // Negative numbers work too
let big = 1000000; // No commas in code! 3. Booleans (True/False)
Booleans are simple: they can only be true or false.
They're perfect for yes/no situations.
let isLoggedIn = true;
let hasPermission = false;
let isAdult = age >= 18; // This becomes true or false based on age 4. Undefined and Null
Sometimes variables don't have a value yet, or we intentionally want them to be empty.
let futureValue; // undefined - not yet assigned
let emptyOnPurpose = null; // null - intentionally empty Try It Yourself
Checking Data Types
You can use the typeof operator to check what type a value is:
typeof "Hello" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined" Type Conversion
Sometimes you need to convert one type to another. JavaScript provides built-in functions for this:
// String to Number
let numStr = "42";
let num = Number(numStr); // 42
// Number to String
let price = 19.99;
let priceStr = String(price); // "19.99"
// To Boolean
Boolean(0) // false
Boolean("hello") // true Quick Quiz
Key Takeaways
- Strings are text in quotes:
"Hello" - Numbers are numeric values:
42or3.14 - Booleans are true/false values
- Use
typeofto check a value's type - You can convert between types using
Number(),String(),Boolean()
What's Next?
Now that you know about data types, let's learn how to work with them using operators—the symbols that let you do math, combine strings, and compare values!
Finished this concept?
Mark it complete to track your progress