Variables and Data Types · Lesson 4 of 40
JavaScript Data Types and typeof
JavaScript is dynamically typed: variables don't have types, values do. The same variable can hold a number now and a string later. Every value is either a primitive (a simple, immutable value) or an object (a collection of properties, passed around by reference).
In this lesson
- The seven primitive types and the object type
typeofand its famous quirksnullvsundefined, NaN and number precision- Primitives vs references
The types
Every JavaScript value
Value
- Primitives (7)string, number, bigint, boolean, undefined, null, symbol
- Objectplain objects, arrays, functions, dates, maps, sets, class instances…
console.log(typeof "hello"); // string
console.log(typeof 42, typeof 3.14); // number – one type for integers and decimals
console.log(typeof 10n); // bigint
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof Symbol("id")); // symbol
console.log(typeof { a: 1 }); // object
console.log(typeof [1, 2]); // object – arrays are objects
console.log(typeof null); // object – a historical bug
console.log(typeof function () {}); // function
string number number bigint boolean undefined symbol object object object function
Common mistake: trusting typeof for arrays and null
typeof null is "object" (a bug from 1995 that can't be fixed without breaking the web) and arrays are also "object". Use value === null and Array.isArray(value) instead.
undefined vs null
let notSetYet;
const user = { name: "Ravi", phone: null }; // deliberately empty
console.log(notSetYet); // a variable without a value
console.log(user.email); // a missing property
console.log(user.phone); // intentionally "no value"
console.log(null == undefined, null === undefined);
undefined undefined null true false
undefined means "not set" – JavaScript gives it to you automatically. null means "intentionally empty" – you assign it yourself.
Numbers
All ordinary numbers are 64-bit floating-point values. That brings two things to know: decimal precision and the special values NaN and Infinity.
console.log(0.1 + 0.2);
console.log((0.1 + 0.2).toFixed(2));
console.log(10 / 3, 10 % 3);
console.log(1 / 0, -1 / 0);
console.log("abc" * 2); // NaN: "not a number"
console.log(NaN === NaN, Number.isNaN("abc" * 2));
console.log(Number.MAX_SAFE_INTEGER);
console.log(2 ** 53 + 1); // precision lost
console.log(2n ** 53n + 1n); // BigInt: exact
0.30000000000000004 0.30 3.3333333333333335 1 Infinity -Infinity NaN false true 9007199254740991 9007199254740992 9007199254740993n
- Integers are exact only up to
Number.MAX_SAFE_INTEGER(2⁵³ − 1). UseBigInt(annsuffix) for larger whole numbers like IDs. NaNis the only value not equal to itself – check it withNumber.isNaN().- For money, store paise as integers or use a decimal library – never trust floating-point sums.
Strings and booleans in brief
const first = 'Ada', last = "Lovelace";
const full = `${first} ${last}`; // template literal
console.log(full, full.length, full[0]);
console.log(typeof (5 > 3), 5 > 3);
Ada Lovelace 12 A boolean true
Strings can use single quotes, double quotes or backticks; backticks allow ${} placeholders and multiple lines. Strings are immutable – methods return new strings (chapter 5).
Primitives vs references
Primitives are copied by value. Objects are copied by reference – both variables point to the same object.
let a = 10;
let b = a; // copies the value
b = 20;
console.log(a, b);
const original = { score: 10 };
const alias = original; // copies the reference
alias.score = 99;
console.log(original.score);
const copy = { ...original }; // a new object (shallow copy)
copy.score = 1;
console.log(original.score, copy.score);
console.log({ x: 1 } === { x: 1 }); // two different objects
10 20 99 99 1 false
Where the values live
a10primitive – its own copyoriginal→ { score: 99 }object #1alias→ same object #1shared referencecopy→ { score: 1 }object #2
Practice
Try it yourself
Write a function
typeName(v)that returns "null", "array" or the typeof result.Show solution
function typeName(v) { if (v === null) return "null"; if (Array.isArray(v)) return "array"; return typeof v; } console.log(typeName(null), typeName([1]), typeName({}), typeName(7));null array object number
Quick check
How many primitive types does JavaScript have?
Seven: string, number, bigint, boolean, undefined, null, symbol.
What is typeof NaN?
"number"
Does const b = a copy an object?
No – it copies the reference; both names point to the same object.
Summary
- Seven primitives plus objects; values have types, variables don't.
typeof nullis "object" and arrays are objects – use=== nullandArray.isArray.- Numbers are floating point (use BigInt for huge integers); objects are shared by reference.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.