Data Types in Programming
Different kinds of values behave differently. Numbers can be added; text can be searched; booleans can be flipped. The "type" tells the computer what operations are valid.
From Bee: JavaScript will silently convert types behind your back. Always use === instead of ==. Always parse form input with Number() before doing math.
What Is a Data Type?
A data type is a category that describes a value. 42 is a number; "hello" is a string; true is a boolean. The type determines which operations make sense — adding two numbers works, but adding two booleans usually doesn't.
Languages handle types in two main ways. Statically typed languages (Java, C++) require you to declare the type up-front — the compiler checks types before running. Dynamically typed languages (Python, JavaScript) figure types out at runtime — more flexible but easier to slip a wrong-type bug past your eyes.
The Core Types You'll See Everywhere
- Number — whole numbers (
42) and decimals (3.14). Some languages split them intointandfloat. - String — text in quotes:
"hello". - Boolean —
trueorfalse. - Array / List — an ordered sequence of values.
- Object / Dictionary — a collection of named fields.
- Null / None — explicit "no value."
- Undefined (JavaScript) — variable declared but not assigned.
Code Examples in Three Languages
// JavaScript types
let n = 42; // number
let s = "hi"; // string
let b = true; // boolean
let arr = [1, 2, 3]; // array
let obj = { x: 1 }; // object
let nothing = null; // null
let maybe; // undefined
console.log(typeof n); // "number"
console.log(typeof s); // "string"# Python types
n = 42 # int
x = 3.14 # float
s = "hi" # str
b = True # bool (capitalized!)
lst = [1, 2, 3] # list
d = {"x": 1} # dict
nothing = None # None
print(type(n)) # <class 'int'>
print(type(s)) # <class 'str'>// Java types — must be declared
int n = 42;
double x = 3.14;
String s = "hi";
boolean b = true;
int[] arr = {1, 2, 3};
// Java has no built-in dict;
// use HashMap<String,Object>
String nothing = null;Best Practices
- Pick the most specific type —
intoverdoubleif you only need whole numbers. - Don't mix types accidentally:
"5" + 3is "53" in JS, an error in Python. - Convert types explicitly:
Number(s)in JS,int(s)in Python. - Prefer
null/Noneover a "magic" sentinel value like-1.
Common Mistakes
- Treating a string number as a number. Form inputs are always strings — convert them first.
- Comparing different types with
==. JavaScript's"5" == 5returns true (use===). Most languages refuse this comparison. - Forgetting that floats aren't exact.
0.1 + 0.2 === 0.3is false — floating-point arithmetic is approximate. - Reading
typeof nullin JS. Returns "object" — a famous quirk. Use=== nullfor null checks.
We have a dedicated Common Bugs with Data Types page — five real broken snippets with the fix. Read it before you start writing.
How It Works Under the Hood
Behind the scenes, every value in your program is stored as bits in memory. The type is metadata that tells the language how to read those bits and which operations are valid. A 4-byte chunk of memory representing the integer 42 looks identical to the same 4 bytes representing a different float — the type is what disambiguates them.
This explains why some languages (Java, Rust) require you to declare types up-front: the compiler needs to allocate the right amount of memory and emit the correct CPU instructions. Dynamic languages (Python, JavaScript) defer that decision to runtime, which is why they can be slower but more flexible.
JavaScript actually uses a single Number type for all numerics — internally, every number is a 64-bit IEEE 754 float. Python automatically promotes integers beyond 64 bits into arbitrary-precision BigInt values. Java keeps a strict separation: int, long, float, double are all distinct.
From Beginner to Pro
The same concept gets deeper as you grow. Here's what mastery looks like at three levels:
You declare a variable and use the literal that matches the type you want: let n = 42, let s = "hi", let b = true.
You convert types explicitly when crossing boundaries: form input Number(input.value), JSON JSON.parse(text), dates new Date(iso). You've noticed that "5" + 3 is "53" in JS and refuse to rely on coercion.
You think in terms of type contracts: every function declares what it accepts and returns (TypeScript, Python type hints, or doc comments). You know that floats are approximate (0.1 + 0.2 ≠ 0.3) and use libraries like Decimal for money. You distinguish null, undefined, missing keys, and empty values deliberately.
Performance & Gotchas
- Float arithmetic is approximate. Use a tolerance for equality, or use a Decimal library for money.
- JS's typeof null === "object" is a 25-year-old quirk. Use
=== null. - Java boxed types (
Integervsint) compare with.equals(), not==. - NaN never equals itself.
NaN === NaNisfalse. UseNumber.isNaN().
Authoritative references: MDN — JavaScript types, Python — built-in types, Oracle — Java primitives, IEEE 754 (float spec).
Quick Quiz
Real-World Uses (Production Code, Today)
Form processing
Every web form input you ever submit comes in as a string. Backends spend real CPU converting strings to numbers, dates, booleans before any business logic runs.
Database columns
Every database column has a type — INT, VARCHAR, BOOLEAN, TIMESTAMP. The type drives storage size, sort order, and which queries are valid.
JSON APIs
When you fetch JSON, the parser maps each value to a JS / Python type: numbers to number, true/false to boolean, [...] to array. Same six types we cover here, exposed at the wire level.
Frequently Asked Questions
What's the difference between an int and a float?
Ints are whole numbers; floats have a decimal part. Floats use more memory and are slightly slower, but can represent fractions.
Why is JavaScript a dynamic language?
By design — easier to write quickly, harder to keep correct. TypeScript adds static type checking on top of JavaScript.
What's the boolean type for?
Storing yes/no, on/off, true/false answers. Conditionals run on booleans.
What is null?
An explicit "there is no value here" marker. Different from "the variable doesn't exist."
What is undefined?
JavaScript-specific: a variable was declared but never assigned a value.
Are strings and characters the same?
In Java they're different (char vs String). In Python and JavaScript a single character is just a 1-length string.
How do I check the type of something?
typeof x in JS, type(x) in Python, x.getClass() in Java.
What's "type coercion"?
Automatic conversion between types — JavaScript does it aggressively, Python rarely. Coercion is a common source of bugs.
Related Concepts
- Data Types in Programming is one of the 13 universal concepts of programming.
- The syntax differs across languages, but the underlying idea is the same.
- Practice in the playground to make it stick.