We are covering the following point:
Data Types Explained | What is a data type?
A data type is all about telling the compiler how to interpret or compile the data. The usage of data types is critical and must be used wisely. In simple words, it is about the format of the data such as Number, String, Boolean and more. However, there are two categories of data types Primitive data types and Non-Primitive data types. There are eight Primitive data types that we will go through in the next section.
Primitive Data Types
Primitive data types are those which are immutable and cannot be changed after the creation and initialization. However, you can re-assign a new value but cannot mutate the existing value.
var name = "my name is mustafa"; name = "my name is changed"; console.log(name) // output: my name is changed name = "M"; console.log(name) // output: my name is changed
In the example above, String is a primitive data type and the old value is being replaced by the new value but the change in the existing value is not being reflected. So, we can see it is still “my name is changed” not “My name is changed”;
Non-Primitive Data Types
Non-Primitive data types are mutable, in simple words opposite of primitive data types.
var arr = [1,2,3]; arr = 11; console.log(arr) // output: [11,2,3]
In addition, primitive data types are compared by value, and non-primitive data types are always compared by reference.
// Primitive Data Type var first_name = "mustafa"; var last_name = "mustafa"; first_name === last_name // true // Non-Primitive Data Type var arr = [1,2]; var arr2 = [1,2]; arr === arr2 // false // BUT var arr = [1,2]; var arr2 = [1,2]; arr = arr2; arr === arr2 // true
Now, let’s go through the data types one by one. There are 6 primitive data types and 1 non-primitive data type.
Primitive Data Types include:
- String: This data type represents textual data which consists of a sequence of characters surrounded by quotes.
- Number: It represents decimal/non-decimal data, particularly integer or float.
- Boolean: Representation of logical values true or false.
- Symbol: Symbol is used to hold unique values.
- Undefined: A variable without any value or an uninitialized variable.
- Null: The absence of any value is considered Null.
Non-Primitive Data Type includes:
Object: It is used to store the collection of data consisting of key-value pairs.
Ask a Question
You can let us know about your questions or queries on our forum.