JavaScript: JavaScript Data Types

Last updated: 2026-10-13

Data types determine what operations you can perform — numbers support arithmetic, strings support concatenation, booleans support conditionals. Understanding types helps you avoid JS's most classic pitfalls.

Related Global Methods:Array.isArray() Number.isNaN() parseInt() Number() typeof

1. JS Data Types

JavaScript has 8 data types, divided into primitives and reference types:

Primitives (7 types): String, Number, BigInt, Boolean, Undefined, Null, Symbol

Reference type (1 type): Object (arrays, functions, dates, etc. are all subtypes of Object)

Primitive data is stored in stack memory and copied by value during assignment. Reference type data is stored in heap memory and copied by reference (address) during assignment. This distinction will be covered in depth in later lessons.


2. The typeof Operator

typeof is an operator that checks data types and returns a string:

Value typeof Result
"hello" "string"
42 "number"
9007199254740991n "bigint"
true "boolean"
undefined "undefined"
null "object" (historical bug)
{ } "object"
function(){} "function"

Note: typeof null returns "object" — this is a historical bug from JS's inception that can never be fixed (changing it would break existing code). Just remember it.


3. String and Number Pitfalls

The + operator in JS serves two purposes: numeric addition and string concatenation. When one side of + is a string, JS converts the other value to a string as well:

This is one of JS's most classic pitfalls — every beginner falls for it. The fix: use Number() or parseInt() for explicit conversion before math operations.


4. null vs undefined

Both mean "no value," but they differ in intent:

Think of it this way: undefined is a new house with no furniture yet; null is when you deliberately moved all the furniture out.

▶ Example: typeof with Various Types

▶ Try it Yourself

▶ Example: String and Number Pitfalls

▶ Try it Yourself

▶ Example: null vs undefined

▶ Try it Yourself

▶ Example: All Data Types at a Glance

▶ Try it Yourself

Related Global Methods:Array.isArray() Number.isNaN() parseInt() Number() typeof

❓ FAQ

Q Why does typeof null return "object"?
A This is a historical bug from JS's first version. Back then, JS used low bits to identify types, and null had all zero low bits — the same as the object identifier — so it was misclassified. They tried to fix it later, but changing it would break too much existing code, so the bug became permanent. It's like a building with a crooked foundation — too many people live there to tear it down.
Q How do I check if a value is an array vs a plain object?
A typeof returns "object" for both arrays and objects, so it can't distinguish them. The correct approach is Array.isArray() — for example, Array.isArray([1,2]) returns true, while Array.isArray({}) returns false.
Q What type is NaN?
A NaN (Not a Number) has typeof NaN returning "number". Even stranger, NaN === NaN returns false — it's not even equal to itself. Use isNaN() or Number.isNaN() to check for NaN.

📖 Summary

📝 Exercises

  1. Basic: Declare 6 variables for 6 primitive types (String, Number, Boolean, Undefined, Null, Object), use typeof on each, and display the results on the page.
  2. Intermediate: Create a page with an input field. When the user enters any value, JS detects its type using typeof and displays the result. Try entering numbers, strings, true, null, etc., and observe typeof's output.
  3. Challenge: Create a "Type Trap Quiz" page with 5 multiple-choice questions testing type coercion knowledge, such as what "1" + 2 equals, whether null == undefined is true or false, etc. Show correct/incorrect feedback with explanations when the user clicks an option.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏