How to Create an Object with new in JavaScript
A class is a blueprint, and new builds an object from it. JavaScript and Node.js come with many classes, such as URL, Date and Map, and libraries add their own; every one of them gives you a working object when you put new in front of it.
Syntax
const <object> = new <ClassName>(<arguments>);
| Part | What it does |
|---|---|
new | Creates a new object and runs the class's constructor to set it up. |
<ClassName> | The class to build from, such as URL. |
<arguments> | The values the constructor needs, such as the address for a URL. |
<object> | The new object, with the class's properties and methods. |
MDN's new operator reference: "The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function."
The Example
The example builds a URL object from a web address and reads three parts of it: the host name, the path and the value of the page query parameter.
Prerequisites
- Node.js. This guide uses Node.js 26.13.1.
Step 1: Write the Code
Create a file named main.js:

const link = new URL('https://example.com/books?page=2');
console.log(link.hostname);
console.log(link.pathname);
console.log(link.searchParams.get('page'));
new URL(...)builds aURLobject. Its constructor takes the address and splits it into parts.link.hostnameandlink.pathnameare properties: values the object holds.link.searchParams.get('page')calls a method: something the object can do.
Step 2: Run It
node main.js
example.com
/books
2

The constructor did the work of splitting the address, so the code only had to ask for the parts. The command is the same in zsh, bash and PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out new, as in URL('https://example.com') | TypeError: Class constructor URL cannot be invoked without 'new' | Write new URL(...) |
Giving the constructor a value it can't use, such as new URL('example.com/books') | TypeError: Invalid URL, because the address has no https:// | Pass a complete address |
Expecting two new calls to give the same object | Each new builds a separate object | Keep one object in a variable and reuse it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: Class constructor … cannot be invoked without 'new' | A class was called like a function | Add new before the class name |
TypeError: Invalid URL with code: 'ERR_INVALID_URL' | The text isn't a complete URL | Include the scheme, such as https:// |
FAQ
How do I know whether something needs new?
Its documentation shows it. Classes such as URL, Date and Map are used with new; plain functions such as parseInt are called without it.
Can I create my own classes?
Yes, with the class keyword, and new builds objects from them the same way.
Conclusion
new ClassName(arguments) builds an object from a class. Its constructor sets the object up from the arguments, and the object then gives you properties to read and methods to call.
