Skip to main content

How to Create an Object with new in JavaScript

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
const <object> = new <ClassName>(<arguments>);
PartWhat it does
newCreates 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:

main.js, which creates a URL object with new URL for https://example.com/books?page=2 and prints its hostname, pathname and the page search parameter.

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 a URL object. Its constructor takes the address and splits it into parts.
  • link.hostname and link.pathname are properties: values the object holds.
  • link.searchParams.get('page') calls a method: something the object can do.

Step 2: Run It​

Terminal
node main.js
Output
example.com
/books
2

A zsh terminal where node main.js prints example.com, /books and 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​

MistakeWhat happensDo 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 objectEach new builds a separate objectKeep one object in a variable and reuse it

Troubleshooting​

SymptomLikely causeFix
TypeError: Class constructor … cannot be invoked without 'new'A class was called like a functionAdd new before the class name
TypeError: Invalid URL with code: 'ERR_INVALID_URL'The text isn't a complete URLInclude 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.

Additional Resources​

  • new, on MDN.
  • URL, in the Node.js documentation.