Skip to content

Javascript Types

You've probably heard the saying "In javascript everything is an object". Well this is a false statement. There is primitive values and there is objects.

Primitive types

1) undefined

Used for unintentionally missing values, usually automatically assigned default value.

You can use it in your own code by writing undefined like you write 3 or "hi". However, undefined also commonly occurs “naturally”. It shows up in situations where JavaScript doesn’t know what value you wanted. For example, if you forget to assign a variable, it will point to undefined.

2) null ⚠️

Used for intentionally missing values, usually explicitly assigned.

Due to a bug in JavaScript, it pretends to be an object.

You might think this means null is an object. It is a primitive value, and it doesn’t behave in any way like an object.

Unfortunately, typeof null is a historical accident that we’ll have to live with forever.


Why have both null and undefined?

In theory this could help us distinguish a coding mistake (which might result in undefined) from valid missing data (which you might express as null).

However, this is only a convention, and JavaScript doesn’t enforce this usage.


3) string

Used for text.

4) number

Used for math calculations.


Numbers in JavaScript are floating point.

JavaScript uses numbers with limited precision. Not all numbers can be perfectly represented in JavaScript. Their decimal part offers more precision closer to 0, and less precision further away from it.

When we write 0.1 or 0.2, we don’t get exactly 0.1 and 0.2. We get the closest available numbers in JavaScript. They are almost exactly the same, but there might be a tiny difference. These tiny differences add up, which is why 0.1 + 0.2 doesn’t give us exactly the same number as writing 0.3.

(Don't fuss about this too much but if you want to read more here is a article from linkedin.)


Special Numbers

Floating-point math includes a few special numbers. You might rarerly run into NaN, Infinity, -Infinity, and -0.

Out of these special numbers NaN is particularly interesting.

It is a historically acronym "Not A Number". It doesn't mean that it is not a number rather it means, it is an invalid number. It come by way of the The IEEE 754 spec.

From JavaScript's perspective NaN is a numberic value. It is a number but it's an invalid number. Saying NaN is 'Not A Number' can be weird but we have to live with it.

The reason this returns false is not a bug. IEEE says NaNs are not equal to each other. We can use isNaN(foo) to check if variables value is NaN.

isNaN("lorem ipsum") returns true since it does coercion. But with ES6 they thought they should change it and Number.isNaN is added. Number.isNaN("foo bar") returns false since it doesn't do coercion.

NaN is the only value in Javascript that does not have what we call the identity property. Meaning it is not equal to itself. So here is a puzzle for you, what should foo be for following expression to be true.

There is one last thing to consider about speacial numbers and that is: -0 === 0 and 0 === -0 are true even though they are differente values.


5) boolean

Used for logical operations.

6) symbol (uncommon)

It’s important to know that Symbols exist, but it’s hard to explain their role and behavior. They are very rare, so we’re going to skip them.

7) bigint (uncommon and new)

Used for math on big numbers. Regular numbers can’t represent large integers with precision, so BigInts fill that gap.


Primitives are immutable

All primitive values are immutable. In other words “unchangeable”, "Read-only". We can’t mess with primitive values, at all.

These will not work, and will throw an error in strict mode.

Primitive values don’t have properties of their own

Even though string has few built-in properties that does not mean they are objects. This properties are inherited from String.prototype (String wraper) as if it's an object. They are temporarily converted to object for being able to access those methods.

An example taken from “The Secret Life of JavaScript Primitives”

There is two things to note here:

1- Our primite value a managed to inherit returnMe using prototypical inheritance.

2- Typeof b is object. This is because string is converted to it's wrapper object String on the fly. returnMe function was able to capture this instance by returning this.

Object types

1) object

Used to group related data and code.

2) function ⚠️

Function is a sub-type of object.

There is a quirk that we need to be careful about. Function's type is actually not function. Functions are regular objects with the additional capability of being callable.

No other types

In JavaScript, there are no other fundamental value types other than the ones we have just discussed. The rest are all objects! For example, arrays, dates, and regular expressions are objects in JavaScript.


Here you can find some useful links:

Profile picture

I have a passion for all things web.