A constructor method in JavaScript is a special function within a class that is called when an instance of the class is created. It is used to initialize the object’s properties.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const person = new Person('John', 30);
console.log(person.name); // John
Tags: basic, JavaScript, classes
URL: https://www.tiktok.com/@jsmentoring/photo/7462819474555653408