Types in JavaScript
Explore the different types in JavaScript, focusing on primitive types like booleans, numbers, strings, symbols, null, and undefined, and their characteristics. Understand the immutability of primitives and the uniqueness of symbols, along with an introduction to objects and functions as non-primitive types.
We'll cover the following...
Types of types
Types in JavaScript group together similar kinds of values. We can further categorize these types into the following:
- Primitive values
- Objects and functions
Let’s look at these in detail.
Primitive values
Primitive values are immutable values in JavaScript.
Before we see an example, let’s forget what immutable means outside of a coding context. Below is a visualization of how an increment operator works.
The above visualization outlines how primitives are immutables. The important takeaway is that a primitive itself cannot be modified, but new primitives can be created, and those values are assigned to variables.
Following are all types that fall under the primitive category:
- Boolean
- Number
- Null
- Undefined
- String
- Symbol
Let’s explore them individually.
Boolean
These are values that take only the values true and false.
In the above code, we can see the two ways of initializing boolean variables on line 1 and 2. Printing their values and types in the following line confirms our initialization.
...