JavaScript Data Types
A variable is a named box. This session asks a sharper question: what kind of thing sits inside the box? Those kinds are data types. You will meet the beginner set, then use typeof to ask JavaScript for a type label.
What are JavaScript data types and how do you check them?
A data type is the kind of value a name holds — text, a number, true/false, “no value yet,” and more. In this course the first map is string, number, boolean, undefined, and null. Objects and arrays appear briefly as “grouped values”; later sessions go deep. To check a value, write typeof before it (or before a variable name). JavaScript answers with a short type string such as "string" or "number".
Beginner type map
📊 Common value kinds beginners meet first
| Kind | Example value | Typical typeof |
Beginner note |
|---|---|---|---|
| String (text) | "Hello" |
"string" |
Text in quotes — single or double |
| Number | 42 or 3.5 |
"number" |
Integers and decimals share one type here |
| Boolean | true / false |
"boolean" |
On/off answers for decisions later |
| Undefined | a name with no value yet | "undefined" |
Declared but not assigned (or missing) |
| Null | null |
"object" (quirk) |
Intentional empty; typeof result looks odd — remember the quirk |
| Object / array (preview) | { } / [1, 2] |
"object" |
Groups of values; details come later |
String values
Text lives in quotes. typeof reports "string".
let label = "JavaScript";
console.log(label);
console.log(typeof label);
Number values
Whole numbers and decimals are both the number type in beginner JavaScript. No separate “integer type” is required here.
let score = 42;
console.log(score);
console.log(typeof score);
Boolean values
true and false are their own type — useful later for conditions. They are keywords, not strings.
let ready = true;
console.log(ready);
console.log(typeof ready);
Undefined — no value yet
If you declare a name with let and never assign, the value is undefined. typeof reports "undefined".
let pending;
console.log(pending);
console.log(typeof pending);
Null — intentional empty
null means “empty on purpose.” Odd fact for beginners: typeof null is "object" — a long-standing quirk. Treat null as empty, and remember that typeof label so it does not confuse you.
let empty = null;
console.log(empty);
console.log(typeof empty);
Check several values in one script
Log both the value and its typeof result. Notice how "10" (quotes) is a string, while 10 is a number.
console.log(typeof "10");
console.log(typeof 10);
console.log(typeof false);
console.log(typeof undefined);
console.log(typeof null);
Show a type label on the page
Store a value, read typeof, and put that label into the page with textContent.
<!DOCTYPE html>
<html>
<head>
<title>Data Types — typeof on page</title>
</head>
<body>
<p id="out">…</p>
<script>
let sample = "W3Learn";
let kind = typeof sample;
document.getElementById("out").textContent = "Type: " + kind;
</script>
</body>
</html>
The paragraph shows Type: string because sample holds text. Change sample to a number or true, refresh, and watch the label update.
Try it yourself
- Create a
letwith a quoted city name; log the value andtypeof. - Assign a number (no quotes); log
typeof— expect"number". - Assign
trueorfalse; confirmtypeofis"boolean". - Declare
let x;with no assignment; logtypeof x. - Assign
null; logtypeofand note the"object"quirk. - In the HTML demo, change only
sample, refresh, and confirm the page type label.
Common mistakes
- Writing
let n = "42";and expecting number math — quotes make a string - Panicking when
typeof nullis"object"— quirk, not a new lesson on objects yet - Treating the words
stringornumberas variable names you must declare - Forgetting
typeofwhen two values “look alike” in the Console but behave differently - Diving into arrays/objects before the later sessions — this map is enough for now
Summary
- Data types are the kinds of values variables can hold
- Beginner map: string, number, boolean, undefined, null (plus a short object/array preview)
typeofreturns a type label string for a valuetypeof nullis"object"— remember the quirk- Next: Numbers — working with numeric values in more detail
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top