Primitive data type means it is not an object and has no methods. All primitives are immutable (the value cant be altered). Let's see the following example:
we declared a primitive variable called result and a function that adds five to the result. The output is zero because addFive function creates a local copy of result and adds five to it and then the local copy variable is destroyed.
Keep in mind that there is a difference between altering the value and reassigning the value of a variable.
let number5 = 5;
number5 = 10; //(number5 variable is reassigned to a new value (10)
Now we are ready, let's back to our main topic.
Have you ever wondered how this line of code even works?
You declared a primitive type (String) but still, you can use toUpperCase method, one of String object methods, how is that possible? another good question, if we can use String methods on str then we can treat it as an object and set properties on it like this one :
But it outputs undefined! sounds weird right?
As you can see, a temporary object wrapper was created when you tried to set the custom property to str, and the result is 1, after that the temporary object wrapper is removed and the custom property doesn’t exist anymore.
Except for null and undefined, all primitive values have object equivalents that wrap around the primitive values.
Thank you for your time, please Don’t hesitate to comment below if you have any questions.