title: What is the difference between JavaScript object and primitive types?
body: <p>Stoyan Stefanov in his excellent book 'Object-Oriented JavaScript' says: </p>
  
  <blockquote>
    <p>Any value that doesn't belong to one of the five primitive types listed above is an object.</p>
  </blockquote>
  
  <p>With five primitive types he means <code>Number</code>, <code>String</code>, <code>Boolean</code>, <code>Undefined</code> and <code>Null</code>. However in Google Chrome console it seems like number is not primitive type at all (compared to <code>C</code> primitive types like <code>int</code>). It looks like the <em>primitive</em> number has methods:</p>
  
  <pre><code>var a = 2.2;
  console.log(a.toFixed()); // logs "2"
  </code></pre>
  
  <p>Thus I assumed that I can work with number as with an object, so I tried to assign a property to it:</p>
  
  <pre><code>var a = 2;
  a.foo = 'bar';
  console.log(a.foo); // logs undefined
  </code></pre>
  
  <p>I don't understand that behavior. If number has a method, it should behave like object, shouldn't it? It even has a prototype:</p>
  
  <pre><code>Number.prototype.foo = 'bar';
  var a = 2;
  console.log(a.foo); // logs 'bar'
  </code></pre>
  
  <p>So what is the <em>magic</em> behind this? How JavaScript treats objects versus <em>primitive</em> types? I would rather not use the word <em>primitive</em> and substitute it with <em>simple objects</em>. As I see it those are objects which can't be extended with new properties, however they are constructed through their constructor and also have prototype which can be extended like with normal objects.</p>