Skip to main content
๐Ÿงฑbeginner

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.

TypeExampleDescription
string"hello"Text
number42, 3.14Integers and floats
booleantrue, falseLogical values
undefinedundefinedVariable declared but not assigned
nullnullIntentional absence of value
symbolSymbol("id")Unique identifier
bigint9007199254740993nVery 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):

hljs javascript
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):

hljs javascript
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:

hljs javascript
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:

hljs javascript
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:

hljs javascript
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:

hljs javascript
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():

hljs javascript
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:

hljs javascript
// + 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:

hljs javascript
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:

hljs javascript
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:

hljs javascript
// 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!
โ–ถTry it yourself

Key Takeaways

  • JavaScript has 7 primitive types + object type
  • typeof null returns "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.

Take the Quiz โ†’