title: Someone please explain this javascript code to me
body: <p>This is the code</p>
  <p>looks like the global <code>user</code> var is affected by function body</p>
  <pre><code>var user =  { name: 'Ivan' };
  
  function setUser(user) {
      console.log(&quot;asdf&quot;, user);
      // user = { name: &quot;asdf&quot;, a: 1 }
      user.name = 'Oleg';
      console.log(&quot;qwer&quot;, user)
  }
  
  setUser(user);
  </code></pre>
  <p>commented adding <code>user = { name: &quot;asdf&quot;, a: 1 }</code> changes result</p>
  <pre><code>function setUser(user) {
      console.log(&quot;asdf&quot;, user);
      user = { name: &quot;asdf&quot;, a: 1 }
      user.name = 'Oleg';
      console.log(&quot;qwer&quot;, user)
  }
  
  setUser(user);
  
  console.log(&quot;1234&quot;, user, user.name);
  </code></pre>
  <p>this is the result ( chrome console )</p>
  <p><a href="https://i.stack.imgur.com/AytOe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AytOe.png" alt="Result" /></a></p>