An object literal is a collection of property names and their corresponding values enclosed in curly braces {}.
{firstName:“John”, lastName:“Doe”, age:50, eyeColor:“blue”}; |
Note: An object literal is also known as an object initializer. |
Create an empty JavaScript object using {}, and then add four properties to it.
// Create an Object const person = {}; // Add Properties person.firstName = “John”; person.lastName = “Doe”; person.age = 50; person.eyeColor = “blue”; |
Create an empty JavaScript object using new Object(), and then add four properties to it.
// Create an Object const person = new Object(); // Add Properties person.firstName = “John”; person.lastName = “Doe”; person.age = 50; person.eyeColor = “blue”; |
Note: The examples above achieve the same result. However, there is no need to use new Object().
For better readability, simplicity, and performance, it’s recommended to use the object literal method. |