JavaScript is designed on an object-based paradigm.
An object is a collection of properties.
Method is a property of an object that contains a function definition.So methods are actions that can be performed on object.
The question that arises here is:
- How we use methods on primitives type🤔?
- How does JavaScript handle them?
Let's go deep into it.🤗✌️
Primitive values
All types except Object define immutable values represented directly at the lowest level of the language. We refer to values of these types as primitive values.
We have 7 primtives type in JS:
- null
- undefined
- string
- number
- bigint
- boolean
- symbol
For example:
let message="hello";
console.log(message.toUpperCase());
We said methods are actions that can be performed on object.
But string is a primitive type so the JS engine highly optimize this process:
JavaScript automatically boxes primitives into relevant objects when it encounters method calls on them.(Boxing)
That method runs and returns a new value.
const message=new String("hello");
- The special object is destroyed, leaving the primitive string alone.(UnBoxing)
The "object wrappers" are different for each primitive type and are called: String, Number, Boolean, Symbol and BigInt. Thus they provide different sets of methods.
Null and undefined have no methods
The special primitives null and undefined are exceptions. They have no corresponding “wrapper objects” and provide no methods. In a sense, they are “the most primitive”.

Top comments (0)