title: What is the use of declaring variable inside constructor
body: <p>I was studying class based structure in Javascript</p>
  
  <p>To say the least, I created a simple class </p>
  
  <pre><code>class something { 
    constructor() {
    var name = "somexyz"
    this.age = 13 
    } 
  
    somefunc () {
      console.log(this.name)//Undefined
      console.log(name) //blank space
      console.log(this.age) //13
    }
  }
  </code></pre>
  
  <p>And then call it using </p>
  
  <pre><code>let foo = new something()
  foo.somefunc()
  </code></pre>
  
  <p>Which consoles.log  (mentioned in comments of above code) 
  1. undefined
  2. empty space 
  3. 13 </p>
  
  <p>or take a constructor function </p>
  
  <pre><code>function something () {
    this.age =  11
    let age = 13 
    this.getAge = function () {
     return this.age
    }
  }
  </code></pre>
  
  <p>Here, </p>
  
  <pre><code>let somethingNew = new something() 
  </code></pre>
  
  <p>Will return age as 11</p>
  
  <p>Now, my question is what exactly is the purpose of having variable in constructors then? </p>