Primitives and objects
JavaScript divides data essentially into two main categories: primitives and objects.
Let's start by defining what a primitive is:
A primitive is one of the simplest forms of data.
A primitive value could be thought of as an atom, if you're familiar with chemistry from the old days at school. An atom is the simplest form of an element, and that's exactly what a primitive is in JavaScript.
The simplest example of primitive values would be numbers. Consider the value 10
. This, as you know, is a number in JavaScript. But most importantly, it's a primitive. It's one of the simplest values that can be represented in JavaScript.
The key characteristic of primitives, as we shall discover later on, is that they don't have any properties/methods attached to them. So when we say that 10
is a primitive value, we, in effect, mean that there are no properties/methods attached to this value 10
.
JavaScript comes equipped with 7 primitive data types: undefined
, null
, numbers, strings, Booleans, Symbols, and BigInt.
For this course, we'll only be concerned with the first five. The remaining two types are covered in our Advanced JavaScript course.
On the other hand, an object is the mirror image of a primitive.
An object is a value composed of primitives.
Going with the analogy that a primitive resembles an atom, an object then resembles a molecule. A molecule is made up of atoms and so are objects, which are made up of primitives.
Simply put, if a value isn't a primitive in JavaScript, then it is an object. Apart form the seven primitive data types discussed above, everything else is an object in the language.
An object, unlike a primitive, can have properties/methods attached to it.
An object is sometimes also referred to as a reference. And by that means, the object type is also known as the reference type. That where does the term 'reference' come from, we'll explore later on in this chapter.
Let's now start exploring the different primitive types in JavaScript before considering its object types.
Last updated