Skip to content
the poor coder

how to create object dynamically in javascript

Poor Man
Published
Reading time
2 min read
On this page

How to Create Object Dynamically in JavaScript#

Creating objects in JavaScript is a crucial part of programming. Object creation is done in two ways:

  • Literal notation
  • Constructor notation

Literal Notation#

The literal notation is the most common way of creating an object in JavaScript. It involves declaring an object using braces {} and assigning values to its properties:

Text
let person = {
    name: 'John',
    age: 30,
    occupation: 'Programmer'
}

You can also add methods to the object using the same notation:

Text
let person = {
    name: 'John',
    age: 30,
    occupation: 'Programmer',
    sayHello: function() {
        console.log('Hello from ' + this.name);
    }
}

Constructor Notation#

The constructor notation is another way of creating an object in JavaScript. It involves creating an object using a constructor function:

Text
function Person(name, age, occupation) {
    this.name = name;
    this.age = age;
    this.occupation = occupation;
}

let person = new Person('John', 30, 'Programmer');

You can also add methods to the object using the prototype property:

Text
function Person(name, age, occupation) {
    this.name = name;
    this.age = age;
    this.occupation = occupation;
}

Person.prototype.sayHello = function() {
    console.log('Hello from ' + this.name);
}

let person = new Person('John', 30, 'Programmer');

Creating Objects Dynamically#

In JavaScript, you can create objects dynamically by assigning properties to an object using square brackets []:

Text
let person = {};
person['name'] = 'John';
person['age'] = 30;
person['occupation'] = 'Programmer';

You can also use the same notation to add methods to the object:

Text
let person = {};
person['name'] = 'John';
person['age'] = 30;
person['occupation'] = 'Programmer';
person['sayHello'] = function() {
    console.log('Hello from ' + this.name);
}

Another way of creating objects dynamically is by using the constructor notation and passing the properties as arguments:

Text
function Person(name, age, occupation) {
    this.name = name;
    this.age = age;
    this.occupation = occupation;
}

let person = new Person('John', 30, 'Programmer');

You can also pass an object containing the properties as an argument:

Text
function Person(person) {
    this.name = person.name;
    this.age = person.age;
    this.occupation = person.occupation;
}

let person = new Person({
    name: 'John',
    age: 30,
    occupation: 'Programmer'
});

In conclusion, creating objects dynamically in JavaScript can be done using either the literal or constructor notation, and by assigning properties or passing them as arguments.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC