JavaScript course menu
JavaScript Data Types: Primitives, Objects and the typeof Operator
Learn the JavaScript data types: the seven primitives (string, number, bigint, boolean, undefined, null, symbol) and objects, dynamic typing, the typeof operator and its quirks, null versus undefined, and primitives versus references.
On this page
Every value in JavaScript has a type. The type decides what you can do with the value: you can multiply numbers, change text to uppercase, or look up an object's properties.
The types#
JavaScript has seven primitive types and one structural type, object:
typeof#
The typeof operator returns the type of a value as a string:
console.log(typeof "Asha");
console.log(typeof 21);
console.log(typeof 21n);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof { city: "Pune" });
console.log(typeof [1, 2, 3]);
console.log(typeof function () {});
console.log(typeof null);string
number
bigint
boolean
undefined
object
object
function
objectTwo quirks to remember:
typeof nullis"object". This is a bug from the first version of JavaScript that can never be fixed without breaking old websites. Check for null withvalue === null.- Arrays are objects, so
typeof []is"object". UseArray.isArray(value)to test for an array.
Functions are objects too, but typeof reports them as "function" for convenience.
Dynamic typing#
Variables do not have fixed types; only values do. The same variable can hold a number and later a string:
let value = 10;
console.log(typeof value);
value = "ten";
console.log(typeof value);number
stringThis flexibility is convenient but makes some mistakes easy, such as passing text where a number was expected. TypeScript adds types to catch those mistakes before the code runs.
undefined and null#
Both mean "no value", but in different ways:
let notSetYet;
const middleName = null;
console.log(notSetYet);
console.log(middleName);
console.log(notSetYet == null, middleName == null);undefined
null
true trueundefinedis what JavaScript gives you automatically: an unassigned variable, a missing function argument, a property that does not exist.nullis what you assign to say "deliberately empty".
value == null (with two equals signs) is true for both, which is a handy way to check for "missing". Comparisons explains == and ===.
Primitives versus objects#
Primitives are copied by value. Objects are shared by reference: copying the variable copies a reference to the same object.
let a = 5;
let b = a;
b = 99;
console.log(a, b);
const original = { score: 5 };
const copy = original;
copy.score = 99;
console.log(original.score, copy.score);5 99
99 99Changing b did not affect a, but changing copy.score changed original.score, because both names refer to the same object. To make a real copy of an object, see Destructuring and Spread.
Primitives are also immutable: string methods such as toUpperCase() return a new string and never change the original.
Practice#
Practice exercise
Predict what each line prints, then run it:
console.log(typeof "42");
console.log(typeof 42);
console.log(typeof NaN);
console.log(Array.isArray([]));
console.log(typeof null === "object");Show solution
console.log(typeof "42");
console.log(typeof 42);
console.log(typeof NaN);
console.log(Array.isArray([]));
console.log(typeof null === "object");string
number
number
true
trueNaN ("not a number") is, confusingly, of type number: it is the number value used for failed calculations.
Key takeaway#
Finished this lesson?