The Primitive data types can neither be an object nor the methods. Also, primitive values cannot be modified since they are “immutable“. You can reassign a variable with a new primitive value but not alter the existing one.
The below-given section will demonstrate the primitive data types in detail.
String primitive data type
Number primitive data type
Bigint primitive data type
“bigint” and “number” data are pretty similar; however, bigint permits you to present the integer values greater than (253). To create a bigint primitive data type value, “n” is appended at the end of the number in the following way:
Boolean primitive data type
Symbol primitive data type
“symbol” is a primitive data type value that can be generated by invoking the “Symbol” function, which returns a “unique” value. The Symbol function accepts a string description as an argument which will be printed out when you retrieve the symbol value.
Undefined primitive data type
The “undefined” primitive data type signifies that a variable is declared but not defined yet.
Null primitive data type
“null” is a data type that is used to represent “missing” or “unknown” values. The “typeof” operator returns “object” as the type of “null”, but remember, null is a primitive value, not an object.
At this point, you have understood what primitive data types are; now, we will learn about the concept behind accessing properties or methods of primitive values.
Another important point to discuss is that the wrapper classes are utilized for storing the same value both externally and internally; however, the instances or objects of the wrapper classes will remain non-primitive in case of explicit object declaration.
let str1 = language.substring(4);
console.log( 'str1 wrapper type is :' + typeof str1);
The above-given code is working perfectly, and now you must be wondering how the “language” variable invoked the “substring()” method that is associated with the string class?
So technically, our executed program is equivalent to this code:
// when language.substring(4) is invoked;
let tmp = new String(language);
str1 = tmp.substring(4);
tmp = null;
Execution of the above-given program will also show the same output:
Now, let’s check out the difference between Manual Primitive Wrapper Object and Automatic Primitive Wrapper Object (Primitive Wrapper type).
Manual Primitive Wrapper Object vs Automatic Primitive Wrapper Object
Example: Manual Primitive Wrapper Object vs Automatic Primitive Wrapper Object
In the below-given example, we will create a manual primitive wrapper object named “str1” using the “new” operator and String() wrapper class constructor:
Whereas, in the case of automatically created primitive wrapper object or primitive wrapper type, the scope is restricted to a single invoked property and method:
str1.language = 'ES6';
The “language” property exists in the memory till this point. Therefore, the output has returned “undefined” instead of the value stored in “str1.language” when the next line is executed: