Primitives and Objects

Updated · 7 min read
mediumES2026javascriptprimitivesobjectswrapper-objectsinterview

Every value in JavaScript is either a primitive or an object. A primitive (a string, number, bigint, boolean, undefined, null or symbol) is a single, immutable value that is copied when assigned. An object is a mutable collection of properties that is shared by reference. Yet you can write "abc".length or (4.5).toFixed(1) as if primitives had properties. This lesson explains both halves of that puzzle, and why it matters for comparisons, function arguments and performance.

In One Minute

  • Primitives are compared and copied by value, and can never be changed in place.
  • Objects (including arrays, functions, dates) are shared by reference and can be changed.
  • "abc".length works because JavaScript briefly wraps the primitive in a wrapper object (String, Number, Boolean, BigInt, Symbol) to look up the property.
  • Never create wrappers yourself with new String(...), new Number(...) or new Boolean(...).
  • String(x), Number(x) and Boolean(x) without new are conversion functions and are fine.

Primitives Are Immutable Values

No operation can change a primitive. Operations produce new primitives:

immutable.js▶ Try it
let name = "navika";
const upper = name.toUpperCase();
console.log(name, upper);
 
let count = 5;
let other = count;
other += 1;
console.log(count, other);
Output
navika NAVIKA
5 6

toUpperCase returned a new string; name still holds the original. other = count copied the number 5, so changing other didn't affect count. Reassigning a variable (name = upper) points the name at a different value; it doesn't modify the old one.

Objects Are Shared References

references.js▶ Try it
const a = { balance: 120 };
const b = a;
b.balance = 80;
console.log(a.balance);
 
function pay(account) {
  account.balance -= 50;
}
pay(a);
console.log(a.balance);
Output
80
30

a and b are two names for one object, and the function received a reference to the same object, so all changes are visible through every reference.

PrimitiveObject
Examples"Navika", 120, true, null{}, [], function () {}, new Date()
Can be changed in placeNoYes
Assignment copiesThe valueThe reference
=== comparesValuesIdentity (same object?)
typeof"string", "number", ... ("object" for null)"object" or "function"
Can hold propertiesNo (see wrappers below)Yes

Wrapper Objects

If primitives can't have properties, how does this work?

wrappers.js▶ Try it
const name = "Srihitha";
console.log(name.length);
console.log(name.toUpperCase());
console.log((4.567).toFixed(2));
console.log(true.toString());
Output
8
SRIHITHA
4.57
true

When you access a property on a primitive, JavaScript temporarily creates a wrapper object for it: a String object for strings, a Number object for numbers, and so on. The property is looked up on that wrapper (which inherits methods like toUpperCase from String.prototype), the result is returned, and the wrapper is thrown away. Engines optimize this away entirely, so it costs nothing in practice.

Because the wrapper is temporary, assigning a property to a primitive goes nowhere:

lost-property.js▶ Try it
const code = "SIBLING";
code.discount = 0.1;
console.log(code.discount);
Output
undefined

The assignment set discount on a temporary wrapper, which was discarded immediately. Reading code.discount creates a new wrapper, which has no such property. (In strict mode, the assignment throws a TypeError instead.)

Never Use new String, new Number or new Boolean

The wrapper constructors can be called with new to create a permanent wrapper object. That object behaves differently from the primitive in confusing ways:

new-wrappers.js▶ Try it
const primitive = "Navika";
const wrapped = new String("Navika");
 
console.log(typeof primitive, typeof wrapped);
console.log(primitive === "Navika", wrapped === "Navika");
console.log(wrapped == "Navika");
 
const flag = new Boolean(false);
if (flag) console.log("new Boolean(false) is truthy!");
Output
string object
true false
true
new Boolean(false) is truthy!

Explanation: new String creates an object, so typeof says "object" and === against the primitive is false. Worst of all, new Boolean(false) is an object, and all objects are truthy, so the if runs. Linters flag these constructors (no-new-wrappers). new BigInt() and new Symbol() simply throw a TypeError.

Calling Them Without new Is Fine

Without new, the same functions convert values to primitives. This is the normal, recommended way to convert:

convert.js▶ Try it
console.log(String(120), typeof String(120));
console.log(Number("45.5"), typeof Number("45.5"));
console.log(Boolean(""), Boolean("no"));
console.log(BigInt("123"), typeof BigInt("123"));
Output
120 string
45.5 number
false true
123 bigint

In Practice

The Fee Tracker normalizes values arriving from a form (all strings) into proper primitives, using conversion functions without new, and keeps the record as an object so it can be updated:

normalize.js▶ Try it
function fromForm(form) {
  return {
    name: String(form.name).trim(),
    grade: Number(form.grade),
    fee: Number(form.fee),
    isActive: form.active === "on",
  };
}
 
const record = fromForm({ name: "  Advitha ", grade: "9", fee: "450", active: "on" });
console.log(record);
console.log(typeof record.grade, typeof record.isActive);
Output
{ name: 'Advitha', grade: 9, fee: 450, isActive: true }
number boolean

Boolean(form.active) would be wrong here: the string "off" is truthy. Comparing to the exact expected string is safer for checkbox values.

Common Mistakes

Creating wrappers with new. new Number(5) === 5 is false and new Boolean(false) is truthy. Use the primitive, or call the function without new.

Adding properties to primitives. They are silently lost. Use an object.

Expecting a function to change a primitive argument. It receives a copy. Return the new value instead.

Expecting === to compare objects by contents. It compares identity.

Under the Hood

How does an engine store values of different types in the same variable? V8 uses a tagged representation. On 64-bit systems with pointer compression (the default in Chrome, and in Node.js builds that enable it), each value slot is 32 bits. If the lowest bit is 0, the slot holds a Smi (small integer) directly, about 31 bits of integer stored right in the slot. If the lowest bit is 1, the slot is a pointer to a heap object.

Everything that isn't a small integer lives on the heap: doubles (as "HeapNumbers"), strings, objects, and even undefined, null, true and false, which are special singleton objects (V8 calls them "oddballs"). So "primitives are values, objects are references" describes the language semantics, not the memory layout: a string variable usually holds a pointer too. What makes a string a primitive is that its contents can never change and === compares contents, so the engine is free to share one copy among many variables. Small-integer arithmetic stays in the fast Smi form; when a number gets too big or gains a fraction, V8 switches it to a HeapNumber, which is one reason integer-heavy loops are fast.

Interview Corner

How can a primitive string have methods like toUpperCase?

Property access on a primitive triggers ToObject: the engine conceptually creates a temporary String wrapper, looks the property up via String.prototype, and discards the wrapper. Engines optimize this so no object is actually allocated in hot code. That's also why properties assigned to primitives disappear.

What is the difference between String("x") and new String("x")?

String("x") is a conversion that returns a primitive string. new String("x") creates a String wrapper object: typeof is "object", it isn't === to the primitive, and it's always truthy. Avoid the new form.

What does this print?

Show solution
puzzle.js▶ Try it
const s = "fee";
s.paid = true;
const n = new Number(0);
console.log(s.paid, typeof n, n ? "truthy" : "falsy", n + 1);
Output
undefined object truthy 1

The property on the primitive is lost. new Number(0) is an object, so it's truthy, though arithmetic unwraps it to 0.

Practice

Challenge: Write toBoolean(value) that converts form strings safely: "true", "on", "yes" and "1" (any case) become true, everything else becomes false. Don't use new Boolean.

Show solution
to-boolean.js▶ Try it
const TRUE_WORDS = new Set(["true", "on", "yes", "1"]);
 
function toBoolean(value) {
  return TRUE_WORDS.has(String(value).trim().toLowerCase());
}
 
console.log(toBoolean("on"), toBoolean("YES"), toBoolean("1"));
console.log(toBoolean("off"), toBoolean("false"), toBoolean(""));
Output
true true true
false false false

Quick check: Primitives and objects

3 questions
1What happens with const s = "abc"; s.x = 1; console.log(s.x);?
2What is typeof new String("hi")?
3Which is the right way to convert "45" to a number?

Key Takeaways

CodeGives
"abc".length3 (via a temporary wrapper)
String(120)"120" (primitive)
new String("a")A wrapper object (avoid)
new Boolean(false)A truthy object (avoid)

Was this tutorial helpful?

Lesson 50 of 139