In JavaScript, primitive types (number, string, etc.) are not objects. So, they don't have a [[prototype]] so can't use methods available in the [[prototype]] of some Objects.
Whereas Number, String are wrapper objects and can be used to create variables with the new
keyword and we can use methods available in the prototype of the Number Object on those variables (created with the new
keyword).
But in the given code, I created a primitive type variable and am able to use methods like toFixed()
which resides in the Number Object.
This is confusing to me. Please elaborate on this.
let a = 6.678; //primitive type
a= a.toFixed(1); // toFixed() resides in prototype of Number Object
console.log(a); // 6.7