Constructor Functions vs Regular Functions
What you'll learn: How any JavaScript function can serve as a constructor when invoked with new, and the conventions that distinguish them.
The Core Difference
Here's a fundamental truth: there's no special syntax that makes a function a "constructor function." Any JavaScript function can act as a constructor if you call it with the new operator. The difference isn't in how the function is written—it's in how it's called.
// This is just a regular function
function createPerson(name) {
return { name: name };
}
// Called normally - acts as a regular function
const person1 = createPerson('Alice');
// The exact same function called with 'new' - acts as a constructor
function Person(name) {
this.name = name;
}
const person2 = new Person('Bob');
Naming Convention: The Only "Real" Difference
Since any function can be a constructor, JavaScript developers use PascalCase (capitalize the first letter) to signal intent:
- Regular functions:
getName(),calculateTotal(),fetchData() - Constructor functions:
Person(),Car(),BankAccount()
This convention warns other developers: "Call this with new!" Without new, a constructor function behaves like any other function—and this won't work as expected, leading to bugs.
Think of It This Way
Imagine a tool that can be a hammer or screwdriver depending on which way you hold it. The tool itself doesn't change; your grip does. Similarly, new is the "grip" that transforms a regular function call into constructor behavior.
Key Takeaway: Constructor functions are just regular functions called with new. The PascalCase naming convention is the only visual signal that a function should be used as a constructor—JavaScript itself doesn't enforce this distinction.