JS Classes, it is showing undefined in result

class Students{
subjectName(name, id){
this.name = name;
this.id = id;
}

		details(){
			document.writeln("Student name is " + this.id + " and id is " + this.name + "<br>");
		}

	}

	var object1 = new Students(15, "Nirdesh Kudeshiya");
	object1.details();

	var object2 = new Students(15, "Nirdesh Kudeshiya");
	object2.details();

you missing rules of class constructor()

subjectName(name, id){ //is wrong change it as below
this.name = name;
this.id = id;
} 

constructor(name, id){ 
this.name = name;
this.id = id;
}

another error is the order of constructor(name, course, id ) should be same while instantiation

var object1 = new Students(10, "Java", "Nirdesh"); // wrong order
1 Like