Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between `new Object()` and object literal notation in JavaScript?
JavaScript provides two main ways to create objects: new Object() constructor and object literal notation {}. While both create objects, they have important differences in syntax, performance, and flexibility.
Object Literal Notation
Object literal notation uses curly braces {} to create objects directly with properties:
let person = {
name: 'Ayush',
age: 25,
city: 'Delhi'
};
console.log(person.name);
console.log(person);
Ayush
{ name: 'Ayush', age: 25, city: 'Delhi' }
Using new Object() Constructor
The new Object() constructor creates an empty object, then properties are added separately:
let person = new Object(); person.name = 'Ayush'; person.age = 25; person.city = 'Delhi'; console.log(person.name); console.log(person);
Ayush
{ name: 'Ayush', age: 25, city: 'Delhi' }
Empty Object Creation
Both approaches can create empty objects:
let obj1 = {};
let obj2 = new Object();
console.log(obj1);
console.log(obj2);
console.log(typeof obj1, typeof obj2);
{}
{}
object object
Key Differences
| Aspect | Object Literal {}
|
new Object() |
|---|---|---|
| Syntax | Concise and clean | More verbose |
| Performance | Faster (less overhead) | Slower (constructor call) |
| Properties | Set during creation | Added after creation |
| Readability | Better for complex objects | Cluttered for multiple properties |
Best Practice Example
Object literal notation is preferred for readability and performance:
// Recommended approach
let car = {
brand: 'Toyota',
model: 'Camry',
year: 2023,
start: function() {
return 'Car started!';
}
};
console.log(car.brand);
console.log(car.start());
Toyota Car started!
Conclusion
While both methods create identical objects, object literal notation {} is preferred due to cleaner syntax, better performance, and improved readability. Use new Object() only when dynamically creating objects or when required by specific patterns.
