Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Order of execution for class in javascript

class Department{   
 constructor(){     
  console.log('parent class')   
 } 
} 
class Employee extends Department{  
  constructor(){    
   super()     
   console.log('child constructor method')   
  }     
  findName(){       
   console.log('Vimal');   
  } 
} 
let E1 = new Employee(); console.log(E1.findName())

Now in the above code it gives me the logs as "parent class" "child constructor method" "User Name" undefined Now where is this undefined coming from can anyone help me understand this

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This function,

findName(){       
   console.log('Vimal');   
  } 

Returns undefined (by default in JavaScript, function that does not explicitly return something – return undefined).

So when you do console.log(E1.findName()), you do 2 things:

  1. Run the function, then Vidal will be logged
  2. Printing the return value of E1.findName(). The function returns undefined so indeed undefined is logged.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading