What means pass-by-value in JavaScript?

January 10, 2022

JavaScript is always pass-by-value. It is obvious to see that primitive variables are passed with values.

But this is not the case for Objects. As seen in the example below, if we assign the firstObj variable to secondObj, they both reference to the same value in memory. Therefore, when we modify firstObj or secondObj, since we are modifying the same reference, we are changing both and show the same value. This should be considered when using the Object.

Helpful resources

Category
HTML
Self-improvement
CSS
JavaScript
TailwindCSS
TypeScript
NextJS
ReactJS
Get In Touch
ozanbatuhankurucu@gmail.com

© 2023