Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Object Definition

Methods for Defining JavaScript Objects

  • Using an Object Literal
  • Using the new Keyword
  • Using an Object Constructor
  • Using Object.assign()
  • Using Object.create()
  • Using Object.fromEntries()

JavaScript Object Literal

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.

Creating a JavaScript Object

Examples

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.