Data Types in JavaScript

Introduction

Every value in JavaScript has a type—number, string, boolean, object, and more. This chapter explains the main types beginners use daily, how to inspect them with typeof, and how dynamic typing affects real programs. Type clarity prevents bugs such as comparing strings when you meant numbers.

Prerequisites

Dynamic Typing

The same variable can hold different types over time (usually avoid doing this without reason):

javascript
// Dynamic typing example (educational, not a daily habit)
let value = 42;
value = "forty-two";
value = true;
console.log(value);

In production code, keep each variable’s meaning stable even though the language allows changes.

Primitive Types (Overview)

TypeExampleTypical use
string"hello"Text, messages, IDs
number42, 3.14Counts, money, scores
booleantrue, falseFlags, conditions
undefinedundefinedMissing value
nullnullIntentional empty value
bigint9007199254740991nVery large integers
symbolSymbol("id")Unique keys (advanced)

Objects (including arrays and functions) are not primitives—you will study them soon.

Strings

Text in single quotes, double quotes, or backticks:

javascript
// String literals
const city = "Austin";
const greeting = 'Hello';
const message = `Welcome to ${city}`;
console.log(message);

Numbers

All numbers are IEEE 754 doubles—integers and decimals share one type:

javascript
// Integer and decimal are both number
const count = 10;
const ratio = 0.75;
const total = count * ratio;
console.log(total);

Booleans

Results of comparisons and logical operations:

javascript
// Boolean from comparison
const age = 20;
const canVote = age >= 18;
console.log(canVote);

undefined and null

  • undefined: not assigned, missing property, or no return value
  • null: deliberate “no value” chosen by the developer
javascript
// undefined vs null
let nickname;
console.log(nickname);
 
const selectedUser = null;
console.log(selectedUser);

Use === null or === undefined when you need strict checks (covered with operators later).

typeof Operator

Inspect a value’s type at runtime:

javascript
// typeof examples
console.log(typeof "hi");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof { a: 1 });

Warning

typeof null returns "object"—a long-standing quirk. For null checks, use value === null instead of relying on typeof.

Objects and Arrays (Preview)

Objects group named properties; arrays are ordered lists—both are object types under the hood:

javascript
// Object and array literals
const user = { id: 1, name: "Lin" };
const scores = [90, 85, 88];
 
console.log(typeof user);
console.log(Array.isArray(scores));

Dedicated chapters explore objects and arrays in depth.

Type Coercion (Preview)

JavaScript sometimes converts types automatically:

javascript
// String + number concatenates
console.log("5" + 2);
 
// Subtraction coerces to number
console.log("5" - 2);

A dedicated type conversion chapter comes later with safer patterns.

Mini Example: Profile Card

javascript
// Mix types in a small profile object
const username = "dev_ada";
const yearsActive = 3;
const isVerified = true;
const bio = null;
 
const profile = {
  username,
  yearsActive,
  isVerified,
  bio,
};
 
console.log(typeof profile.username);
console.log(profile);

FAQ

How many types does JavaScript have?

Commonly seven primitives plus objects. Arrays, functions, dates, and maps are specialized objects.

Is JavaScript strongly or weakly typed?

Often described as dynamically typed with coercion—types are attached to values, not variable declarations, and the engine may convert types in expressions.

Should I use Number or parseInt for user input?

User input arrives as strings. Parse explicitly when you need numbers—covered with input and operators in later chapters.

What is NaN?

“Not a Number” from invalid numeric operations. typeof NaN is "number".

What comes next?

Scope, strings, numbers, and operators in the following chapters.