Primitive Data types
Primitive data types are the simplest kinds of value in JavaScript: single pieces of data that are not objects and have no methods of their own. There are seven of them: string, number, bigint, boolean, undefined, null and symbol. Everything else, including arrays and functions, is an object. Knowing which type a value has tells you how it will compare, copy and behave in calculations.
The seven primitives at a glance
| Type | Example | Used for |
|---|---|---|
string | 'Delhi', `Hi ${name}` | Text |
number | 42, 3.14, NaN | Integers and decimals, one shared type |
bigint | 9007199254740993n | Whole numbers too large for number |
boolean | true, false | Yes or no decisions |
undefined | let x; | A value that has not been assigned |
null | let user = null; | Intentionally empty |
symbol | Symbol('id') | Unique keys that never clash |
Checking a type with typeof
typeof 'hello'; // "string"
typeof 7.5; // "number"
typeof 10n; // "bigint"
typeof false; // "boolean"
typeof undefined; // "undefined"
typeof Symbol(); // "symbol"
typeof null; // "object" (a historical bug, kept for compatibility)
0.1 + 0.2; // 0.30000000000000004
(0.1 + 0.2).toFixed(2); // "0.30"
Number.isNaN(Number('abc')); // true
let a = 'pen';
let b = a; // b gets its own copy of the value
b = 'pencil';
console.log(a); // "pen"
Because typeof null reports "object", check for null directly with value === null.
Primitives are immutable and copied by value
A primitive value can never be changed in place. Methods like 'hello'.toUpperCase() return a new string; the original stays the same. When you assign a primitive to another variable, the value is copied, so changing one variable does not affect the other, as the a and b example shows. Objects behave differently: assigning one copies a reference to the same object.
You may wonder how 'hello'.length works if primitives have no properties. JavaScript briefly wraps the primitive in a matching object, such as String, so the method or property can be used, then throws the wrapper away.
Surprises with numbers
All ordinary numbers are stored as 64-bit floating point, so some decimals cannot be represented exactly. That is why 0.1 + 0.2 is not exactly 0.3. For money, a common approach is storing amounts as whole paise or cents and formatting only for display.
NaN ("not a number") is a number that results from failed conversions like Number('abc'). It is not equal to anything, not even itself, so test it with Number.isNaN(). Integers are only exact up to Number.MAX_SAFE_INTEGER; beyond that, use bigint, though you cannot mix bigint and number in arithmetic without converting.
Type conversion can surprise you as well. The + operator joins strings, so '5' + 2 gives '52', while '5' - 2 gives 3. Convert input explicitly with Number() or String() before doing arithmetic.
undefined versus null
Both mean "no value", but they come from different places. JavaScript gives you undefined automatically for unassigned variables, missing properties and missing function arguments. null is something you assign deliberately to say "empty on purpose". Using null for your own empty values keeps the meaning clear.
Remember
There are seven primitives, they are copied by value and never mutated, typeof null lies, and decimal arithmetic needs care.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.