Primitive Data Types
Usually, number, string, symbol, Boolean, null and undefined are known as primitive data types. This means these data types are already predefined and cannot be mutable. These data types can only be referred to as a single value. It cannot contains multiple values in the code. Let’s clarify the concept with a simple example
In the example we can see when we create a variable with assigning a value, the variable reserved a space in the memory where it holds the value which is assigned to it.
And when we reassigned the variable with a new value, it does not override or change its memory reference rather the variable reserved a new space in the memory for the new value.
And when we check both variables with triple equal with the same value, it returns true. According to the result, we understood that when we check variables that are primitive data types, it checks the assignment value, not its memory location or address.
Non-Primitive Data Type
When we check both variables it returns true because both of the objects contain the same address of memory.
Let’s see another example
From the picture we can see when we declare the objects separately with the same key-value pair and checking them with triple equal it returns false. But why! Both variables have the same value. What’s wrong then?????
Hmm, the answer is when we check the variables of non-primitive data types, it will check the memory address or reference of those variables not the key-value pairs of the objects.
In the image, we can also see that NaN is a number type and can be used to check whether a value is non-numeric. And when we try to divide a value that is greater than 0 and trying to divide by 0 it will return infinity.
A string can be a group of characters or a single character enclosed by a single, double quotation. But in ES6 we can write strings using template literals. The benefits of using it we can change value dynamically in the string. And like number type we can create a String object using a new operator and String constructor.
See the Example
The symbol is a new data type in es6. Normally symbol is used to define a property of an object which is ideally kept private. Another reason for using the symbol data type is to hide the implementation details of an object. It works as a key of key-value pair of an object. If we can add a property using symbol data type we can do this using Symbol() function. Remember that the symbol data type cannot be used as a constructor. Let’s have a look at the example
The data type normally uses to check a logical condition. It has two values true and false. It returns a value based on condition if the condition met successfully it will return true else false. We can also check the type of true and false. Let’s see an example
Data type undefined means the variable is declared but there is no value assigned to the variable. It also defines the absence of value. But undefined can occur in various cases such as
· If we don’t return anything from a function based on a function statement.
· If we declare a variable but didn’t assign a value.
· Call a function with insufficient parameter value then it will return undefined.
· If we assign an object property value to a variable that is not in the object then it will return undefined.
Let’s see an example
Without those points, there are so many reasons to return undefined.
Difference between null and undefined
Null and defined both are used to define the absence of values or empty values. let differentiate them
- Null is used as an ideal value.
- Undefined can also be used as a value but it is bad practice
- Null itself is a value with null which defines no value
- Undefined is normally occur when its value is not assigned yet
- A variable with no value simply means that it is undefined
- A variable with no value didn’t mean its value is null
- The data type of a variable which doesn’t hold a value ‘Undefined’
- But the data type of a variable contains a value null. Which is an object instead of null.
The Non-Primitive Data type — Object
An array is a collection of data or elements. The elements can be numeric data, objects, etc. Array maintains an index for each element in the array. So it contains its value in a key-value pair like an object. But remember that we cannot mutate an array. Let see an example
From the image, we understood that the address in the memory of an array or object keeps immutable but it holds its address and now it contains the new values as you can see in the picture.
- A function is also an object data type and like a null data type if we find the data type of a function it should return the object data type but it returns a function.
- Lack of knowing the correct usage of null and undefined data type
Thanks to all of you to read this article. Please give me feedback to improve my writing skills.