A regular function executes code and can return a value. A constructor function is the same function but used to create objects with the [new] keyword. Main differences:
// Regular function
function greet(name) {
return 'Hello, ' + name;
}
greet('John'); // 'Hello, John'
// Constructor function
function User(name) {
this.name = name;
}
const user = new User('John');
console.log(user.name); // 'John'The difference between a regular function and a constructor function is like the difference between a regular spoon and a scoop. Both items are similar, but used for different purposes! 🥄
A regular function is a block of code that can be called to perform some actions or calculations:
function add(a, b) {
return a + b;
}
const result = add(5, 3); // 8A constructor function is a regular function that is used to create objects:
function Car(brand) {
this.brand = brand;
}
const myCar = new Car('Toyota');
console.log(myCar.brand); // 'Toyota'function Example() {
this.value = 'test';
}
// Regular call
const regular = Example(); // undefined (in strict mode)
// Call as constructor
const constructor = new Example(); // object { value: 'test' }function Test() {
this.name = 'Test';
}
// In regular function this depends on context
Test(); // this → window/undefined
// In constructor this is always a new object
new Test(); // this → {} (new object)function Regular() {
return 'result';
}
function Constructor() {
this.value = 'object';
// automatically return this
}
Regular(); // 'result'
new Constructor(); // object { value: 'object' }// Does something and returns result
function calculateTax(price) {
return price * 0.2;
}
const tax = calculateTax(100); // 20// Creates objects of the same type
function Product(name, price) {
this.name = name;
this.price = price;
}
const product1 = new Product('Book', 500);
const product2 = new Product('Pen', 50);
console.log(product1.name); // 'Book'
console.log(product2.name); // 'Pen'// For performing actions
function showMessage(message) {
console.log(message);
}
// For calculations
function sum(a, b) {
return a + b;
}// For creating similar objects
function User(name, email) {
this.name = name;
this.email = email;
}
// Create users
const user1 = new User('John', 'john@example.com');
const user2 = new User('Peter', 'peter@example.com');function Animal(name) {
this.name = name;
}
// ❌ Error — called constructor as regular function
const animal = Animal('Lion');
console.log(animal); // undefined
// ✅ Correctly — used new
const animal2 = new Animal('Lion');
console.log(animal2.name); // 'Lion'function CreateCounter() {
this.count = 0;
}
// ❌ Will be error
// const counter = CreateCounter(); // undefined
// counter.count++; // Cannot read property 'count' of undefined
// ✅ Correct
const counter = new CreateCounter();
counter.count++; // worksUnderstanding the difference helps properly choose when to use regular functions and when constructors. This is important for writing clean and understandable code! 💪
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪