Data Types
Variables can hold different values and these values can be broken down into different categories known as Data Types. In JavaScript, data types are divided into two categories Primitive Data Types and Objects.
Primitives
Primitives define immutable values or a value that cannot be changed. This means the specific value cannot change, not the variable holding that value. For example, the value of the Number
2 is always 2, you cannot change that. Likewise, the String
of characters ABC
cannot be changed.
In JavaScript, there are six primitive data types, which are as follows:
- Boolean is logical type and has only two values
true
orfalse
- Number is numeric datatype with positive or negative integers with or without decimals
- String is sequence of characters representing text
- Symbol returns a unique
symbol
to be used for object keys - Null is nonexistent value
- Undefined is assigned to a variable that is declared with no value
From this list, the three most common data types are Boolean
, Number
, and String
.
Objects
In JavaScript, an object is considered to be a collection of properties. These properties are stored in key-value pairs. The key is used to identified properties, and values can be a value of any type.
Any data structure that is not a primitive data type is an object including:
- Arrays allows to store multiple items under one variable name
- Objects are used to store keyed collections and complex entities
- Functions are used to define a function to be performed
- Maps contains the key-value pairs retaining the insertion order of the keys
- Sets are collections of unique values
We will learn more about these data types later in the course.