Facebook Pixel

Are primitives stored on the stack or heap in JS?

Primitives like number, string, and boolean are stored directly where the variable lives, typically on the call stack. They copy by value on assignment.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in How JavaScript Allocates Memory for Variables and Functions

Objects, arrays, and functions are stored on the heap. The variable holds a reference (pointer) to that heap location, which is why copying a variable shares the same object.

Primitives copy by value, so changes to one variable do not affect the other. Objects copy by reference, so two variables can point to the same object and mutations are visible through both.

When an object on the heap has no references pointing to it, it becomes unreachable. The garbage collector uses mark-and-sweep to identify and free such objects.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.