JavaScript: pass by value or by reference? Understanding the confusion
Hello everyone! I'm a beginner in JavaScript and I can't understand how argument passing actually works in functions. Some articles say that JS passes everything by value, others say that objects are passed by reference. I wrote a simple code that completely confused me.
Here's an example:
```javascript
function changeValue(x) {
x = 10;
console.log('Inside function:', x);
}
let a = 5;
changeValue(a);
console.log('After function:', a); // 5 — clear, the number didn't change
```
This is clear — a primitive is passed by value. But when I try with an object:
```javascript
function changeObject(obj) {
obj.name = 'New name';
}
let user = { name: 'Old name' };
changeObject(user);
console.log(user.name); // 'New name' — the object changed! So,