JavaScript Object Constructors

Last Updated : 16 Jan, 2026

An object in JavaScript is a collection of related data and functionality stored as key–value pairs, where values can be variables or functions (methods).

  • Objects group related properties and behaviors into a single structure.
  • All JavaScript values are objects except primitive types (string, number, boolean, null, undefined, symbol).
const GFG = {
subject : "programming",
language : "JavaScript",
}
  • GFG is an object created using object literal syntax.
  • subject is a key with the value "programming".
  • language is a key with the value "JavaScript".
  • Each key–value pair represents a property of the GFG object.

Object Constructor

A constructor function in JavaScript is a special function used with the new keyword to create and initialize objects of a specific type, allowing multiple instances with similar structure but unique properties.

  • Used with the new keyword to create object instances
  • Initializes object properties and methods
  • Helps create multiple objects of the same type efficiently
  • Each instance has its own separate data while sharing the same blueprint
JavaScript
//Driver Code Starts
// Constructor function
//Driver Code Ends

function Person(name, age) {
    this.name = name;
    this.age = age;
    this.sayHello = function() {
        console.log(`My name is ${this.name} and I am ${this.age} years old.`);
    };
}

//Driver Code Starts

//Creating Instances with a Constructor
const p1 = new Person("Akash", 30);
const p2 = new Person("Anvesh", 25);

p1.sayHello();
p2.sayHello();
//Driver Code Ends

this keyword

The this keyword in JavaScript refers to the object associated with the current execution context, similar to OOP languages like C++, C#, and Java, and its value depends on how a function is called.

  • this refers to the object linked to the current execution context, similar to OOP languages like C++, C#, and Java.
  • Its value depends on how and where a function is called, not where it is defined.
  • It allows functions and methods to access the object or data member that invoked them.

Adding Property to an Object

The property can be added to the object by using dot(.) operator or square bracket.,

const GFG = {
articles: 'computer science',
quantity: 3000,
};

The GFG has two properties "articles" and "quantity". Now we wish to add one more property name called subject.

Using dot (.) operator

GFG.subject: 'JavaScript';

Using square bracket:

GFG['subject']: 'JavaScript';

Adding Properties in a Constructor Function

Here, subject is the property and "JavaScript" is its value, and properties in a constructor must be defined inside the constructor itself.

  • subject acts as the key, while "JavaScript" is the assigned value.
  • New properties cannot be added like object properties and must be declared within the constructor.
function GFG(a, b, c) {
this.A = a;
this.B = b;
this.C = c;
this.G = "GEEK";
}

Here, we add a property name G with value "GEEK", in this case the value "GEEK" is not passed as an argument.

Adding a Method to an Object

We can add a new method to an existing object.

GFG.n = function () {
return this.A + this.B;
};

Here, the object is GFG.

Adding a Method to Constructor

function GFG(a, b, c) {
this.A = a;
this.B = b;
this.C = c;
this.n = function () {
return this.A + this.B;
}
}

Here, in the last line a method is added to an object. 

Instantiating an object constructor

There are two ways to instantiate object constructor,

const object_name = new Object(); // or  
const object_name = new Object("java", "JavaScript", "C#");

const object_name = { };
  • 1st Method: The object is created using the new keyword (similar to OOP languages), where "Java", "JavaScript", and "C#" are passed as arguments to the constructor.
  • 2nd Method: The object is created directly using object literal syntax with curly braces { }.

Assigning properties to the objects: There are two ways to assigning properties to the objects.

  • Using dot (.) operator:
object_name . properties = value;
  • Using third bracket:
object_name [ 'properties'] = value;

[Example 1]: Object creation by using new keyword and assigning properties to the object using dot(.) operator. 

javascript
let gfg = new Object();

gfg.a = "JavaScript";
gfg.b = "GeeksforGeeks";


//Driver Code Starts
console.log("Subject: " + gfg.a);
console.log("Author: " + gfg.b );
//Driver Code Ends

[Example 2]: Object creation using curly braces and assigning properties to the object using third bracket "[]" operator. 

javascript
let gfg = { };

gfg['a'] = "JavaScript";
gfg['b']= "GeeksforGeeks";


//Driver Code Starts
console.log("Subject: " + gfg.a);
console.log("Author: " + gfg.b );
//Driver Code Ends

[Example 3]: This example shows how to use function() with object constructor. 

javascript
let gfg = new Object();

gfg.a = "JavaScript";
gfg.b = "GeeksforGeeks";
gfg.c = function () {
    return (gfg.a +" "+ gfg.b);
};


//Driver Code Starts
console.log("Subject: " + gfg.a);
console.log("Author: " + gfg.b);
console.log("Adding the strings: "+ gfg.c() );
//Driver Code Ends

[Example 4]: Another way to create a function using function name. 

javascript
let gfg = { };

gfg.a = "JavaScript";
gfg.b = "GeeksforGeeks";
gfg.c = add;

// Declare function add()
function add() {
    return (gfg.a +" "+ gfg.b);
};


//Driver Code Starts
console.log("Subject: " + gfg.a);
console.log("Author: " + gfg.b);
console.log("Adding the strings: "+ gfg.c());
//Driver Code Ends
Comment

Explore