Types in JavaScript
Overview of all the types in JavaScript.
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
.
var bool_true = true; // initialize variable to truevar bool_false = new Boolean(false); // initialize variable to false// print valuesconsole.log('The values are:',bool_true,bool_false);// print typesconsole.log('The types are:',typeof(bool_true),typeof(bool_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.
...