JavaScript: JavaScript Data Types

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Gray🏷 Category: JavaScript Tutorial

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 Data Types:Array.isArray() parseInt() Number() boolean typeof string object symbol

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 Data Types:Array.isArray() parseInt() Number() boolean typeof string object symbol

❓ FAQ

Q What data types does JavaScript have, and what are the 8 basic data types?
A JS has 8 data types: 7 primitive types string, number, boolean, null, undefined, symbol, and bigint, plus the reference type object (arrays, functions, dates, etc. all belong to object). You can use typeof to detect most types, but note that typeof null returns "object" and typeof on a function returns "function".
Q Why does typeof null return "object", and how do you correctly check for null?
A This is a historical bug from the first version of JS: types were identified by low bits, and null's low bits were all 0, the same as object's identifier, so it was misclassified. Fixing it later would break a lot of existing code, so it had to stay. To check for null, use value === null directly rather than relying on typeof.
Q What is the difference between null and undefined, and which should I use in practice?
A undefined means "not assigned" and is the default value when a declared variable is uninitialized, a function returns nothing, or a nonexistent property is accessed; null means "empty value" and must be assigned by the developer to intentionally clear something. In practice, let JS produce undefined automatically and assign null when you need to explicitly clear a reference; when checking, use === null and === undefined to avoid == conflating the two.

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

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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