JavaScriptEasy
How do you create a constructor function?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
To create a constructor function in JavaScript, define a regular function with a capitalized name to indicate it's a constructor. Use the this keyword to set properties and methods. When creating an instance, use the new keyword.
js
function Person(name, age) {
this.name = name;
this.age = age;
}
const john = new Person("John", 30);
console.log(john.age); // 30
