Data Types
JavaScript has 8 data types. Learn what they are, how typeof works, and why type coercion can surprise you.
JavaScript's 8 Data Types
Every value in JavaScript has a type. There are 7 primitive types and 1 object type.
| Type | Example | Description |
|---|---|---|
string | "hello" | Text |
number | 42, 3.14 | Integers and floats |
boolean | true, false | Logical values |
undefined | undefined | Variable declared but not assigned |
null | null | Intentional absence of value |
symbol | Symbol("id") | Unique identifier |
bigint | 9007199254740993n | Very large integers |
object | {}, [], null* | Collections and more |
*null has type "object" โ this is a historical bug in JavaScript.
Strings
Strings represent text. You can use single quotes, double quotes, or template literals (backticks):
const single = 'Hello';
const double = "World";
const template = `Hello, ${double}!`; // template literal
console.log(template); // "Hello, World!"
// String length
console.log("JavaScript".length); // 10
// Common string methods
const str = " Hello World ";
console.log(str.trim()); // "Hello World"
console.log(str.toLowerCase()); // " hello world "
console.log(str.includes("World")); // true
console.log(str.split(" ")); // ["", "", "Hello", "World", "", ""]
Numbers
JavaScript uses a single number type for both integers and floating-point values (IEEE 754 double precision):
const age = 30;
const price = 9.99;
const negative = -5;
const scientific = 1.5e6; // 1,500,000
// Math operations
console.log(10 + 3); // 13
console.log(10 - 3); // 7
console.log(10 * 3); // 30
console.log(10 / 3); // 3.3333...
console.log(10 % 3); // 1 (remainder)
console.log(2 ** 10); // 1024 (exponentiation)
// Special values
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(0 / 0); // NaN (Not a Number)
โ ๏ธFloating point precision
Be careful with decimals โ they can produce surprising results:
console.log(0.1 + 0.2); // 0.30000000000000004
This is a fundamental property of IEEE 754 floating point, not a JavaScript bug. For money, use integer cents or a library like decimal.js.
Booleans
Booleans have only two values: true or false:
const isActive = true;
const hasError = false;
console.log(isActive && hasError); // false (AND)
console.log(isActive || hasError); // true (OR)
console.log(!isActive); // false (NOT)
undefined vs null
These are two distinct "empty" values:
let x; // declared but not assigned
console.log(x); // undefined
let y = null; // explicitly "nothing"
console.log(y); // null
// null is intentional emptiness
// undefined usually means "hasn't been set yet"
โ When to use null vs undefined
Use null when you intentionally want to represent "no value." Let undefined occur naturally for variables not yet assigned. Don't explicitly set things to undefined.
The typeof Operator
typeof tells you a value's type as a string:
typeof "hello" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" โ historical bug
typeof {} // "object"
typeof [] // "object" โ arrays are objects
typeof function(){} // "function"
typeof Symbol() // "symbol"
typeof 42n // "bigint"
To check for arrays, use Array.isArray():
const arr = [1, 2, 3];
console.log(typeof arr); // "object" โ misleading!
console.log(Array.isArray(arr)); // true โ correct
Type Coercion
JavaScript automatically converts types in certain situations โ this is called implicit coercion. It can be a source of bugs:
// + with a string converts numbers to strings
console.log("5" + 3); // "53" (string concatenation!)
console.log("5" + 3 + 1); // "531"
// Other operators try to convert to numbers
console.log("5" - 3); // 2 (numeric subtraction)
console.log("5" * 2); // 10
console.log("abc" - 1); // NaN
// Truthy/falsy coercion
if ("hello") console.log("truthy"); // runs
if (0) console.log("truthy"); // doesn't run
Falsy values โ these are all treated as false in a boolean context:
false, 0, -0, 0n, "", null, undefined, NaN
// Everything else is truthy, including "0", [], {}
โ ๏ธUse === not ==
== performs type coercion before comparison, which leads to unintuitive results:
0 == "0" // true โ coerces!
0 == false // true โ coerces!
null == undefined // true โ coerces!
// Always use === (strict equality)
0 === "0" // false โ
0 === false // false โ
Explicit Type Conversion
You can convert types intentionally:
// To number
Number("42") // 42
Number("3.14") // 3.14
Number(true) // 1
Number(false) // 0
Number("") // 0
Number("abc") // NaN
parseInt("42px") // 42 (stops at non-digit)
parseFloat("3.14rem") // 3.14
// To string
String(42) // "42"
String(true) // "true"
(42).toString() // "42"
// To boolean
Boolean(0) // false
Boolean("") // false
Boolean(null) // false
Boolean("hello") // true
Boolean({}) // true โ even empty objects!
Key Takeaways
- JavaScript has 7 primitive types + object type
typeof nullreturns"object"โ a known bug- Use
Array.isArray()to check for arrays - Always use
===(strict equality) to avoid coercion surprises undefined= not yet set;null= intentionally empty- Falsy values:
false,0,"",null,undefined,NaN
Ready to test your knowledge?
Take a quiz on what you just learned.