In JavaScript, you work with primitive values (like strings, numbers) but still call methods on them.
That's possible because of boxing (wrapper objects).
π― 1. What are Natives?
Natives = built-in types and functions provided by JavaScript
πΉ Primitive (Native Types)
"hello" // string
42 // number
true // boolean
null
undefined
Symbol()
BigInt(10)
π These are primitive values
π They are not objects
πΉ Object Natives
Object
Array
Function
Date
RegExp
π These are actual objects / constructors
π§© 2. The Problem
Primitives are not objects, so:
let str = "hello";
π This should NOT work:
str.toUpperCase(); // β how?
Because:
"hello" is NOT an object
βοΈ 3. What is Boxing (Wrapper Objects)?
Boxing = temporarily converting a primitive into an object so you can use methods
π¬ Behind the Scene
let str = "hello";
str.toUpperCase();
Internally becomes:
new String("hello").toUpperCase();
π JS engine automatically wraps the primitive
π§ This is called Autoboxing
π Full Internal Flow
Primitive β Wrapper Object β Method Call β Discard Wrapper
π§ͺ Example
let num = 10;
num.toFixed(2);
Internally:
new Number(10).toFixed(2);
π Then wrapper is destroyed
βοΈ 4. Types of Wrapper Objects
π§© 5. Important Difference
Primitive
let a = "hello";
Wrapper Object
let b = new String("hello");
β οΈ These are NOT the same
typeof a // "string"
typeof b // "object"
β οΈ Comparison Problem
"a" === new String("a") // false β
π Because:
primitive vs object
π¬ Deep Internal Behavior
When you access a method:
"hello".length
Engine does:
1. Wrap primitive β String object
2. Access property
3. Return value
4. Destroy wrapper
π§ Why Boxing Exists
To allow:
"hello".toUpperCase()
(10).toFixed(2)
true.toString()
π Without making primitives heavy objects
β‘ 6. Unboxing (Reverse Process)
When needed, object converts back to primitive:
let x = new Number(10);
x + 5 // 15
π Uses:
valueOf()
toString()
π§© Example
let obj = new Number(10);
obj.valueOf(); // 10
β οΈ 7. Why You Should NOT Use Wrapper Objects
β Bad Practice
let str = new String("hello");
Problems:
- Unexpected behavior
if (new Boolean(false)) {
console.log("true"); // runs β
}
π Objects are always truthy
- Performance overhead Object creation Memory usage
- Confusing comparisons
π§ 8. Primitives vs Objects Summary

π₯ Real Example (Step-by-step)
"abc".includes("a");
Internally:
1. "abc" β new String("abc")
2. call includes()
3. return true
4. destroy wrapper
π§ Mental Model
βJS temporarily puts primitives inside objects when neededβ
π― Final Takeaways
Natives = built-in types (primitive + object)
Boxing = wrapping primitive into object temporarily
Happens automatically (autoboxing)
Wrapper objects (new String) should be avoided

Top comments (0)