Skip to content
Laviyx Learn

Try

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.

Beginner6 min readUpdated
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:

TypeExamplesMeaning
string"hello", 'a', `hi`Text
number42, 3.14, -7, NaN, InfinityWhole and decimal numbers
bigint9007199254740993nIntegers of any size
booleantrue, falseYes or no
undefinedundefinedA value that has not been set
nullnullIntentionally "no value"
symbolSymbol("id")A unique identifier (advanced)
object{ name: "Asha" }, [1, 2], functions, datesCollections and everything else

typeof#

The typeof operator returns the type of a value as a string:

JavaScript
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);
Output
string
number
bigint
boolean
undefined
object
object
function
object

Two quirks to remember:

  • typeof null is "object". This is a bug from the first version of JavaScript that can never be fixed without breaking old websites. Check for null with value === null.
  • Arrays are objects, so typeof [] is "object". Use Array.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:

JavaScript
let value = 10;
console.log(typeof value);
value = "ten";
console.log(typeof value);
Output
number
string

This 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:

JavaScript
let notSetYet;
const middleName = null;

console.log(notSetYet);
console.log(middleName);
console.log(notSetYet == null, middleName == null);
Output
undefined
null
true true
  • undefined is what JavaScript gives you automatically: an unassigned variable, a missing function argument, a property that does not exist.
  • null is 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.

JavaScript
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);
Output
5 99
99 99

Changing 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:

JavaScript
console.log(typeof "42");
console.log(typeof 42);
console.log(typeof NaN);
console.log(Array.isArray([]));
console.log(typeof null === "object");
Show solution
JavaScript
console.log(typeof "42");
console.log(typeof 42);
console.log(typeof NaN);
console.log(Array.isArray([]));
console.log(typeof null === "object");
Output
string
number
number
true
true

NaN ("not a number") is, confusingly, of type number: it is the number value used for failed calculations.

Key takeaway#

Finished this lesson?