Loading...

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".

String and typeof

javascript
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.

Number and typeof

javascript
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.

Boolean and typeof

javascript
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".

Undefined value

javascript
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.

null and the typeof quirk

javascript
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.

Several typeof checks

javascript
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.

Page shows typeof of a value

html
<!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

  1. Create a let with a quoted city name; log the value and typeof.
  2. Assign a number (no quotes); log typeof — expect "number".
  3. Assign true or false; confirm typeof is "boolean".
  4. Declare let x; with no assignment; log typeof x.
  5. Assign null; log typeof and note the "object" quirk.
  6. 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 null is "object" — quirk, not a new lesson on objects yet
  • Treating the words string or number as variable names you must declare
  • Forgetting typeof when 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)
  • typeof returns a type label string for a value
  • typeof null is "object" — remember the quirk
  • Next: Numbers — working with numeric values in more detail

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
8
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 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