You will discover various methods for creating JavaScript objects in this example.

You should be familiar with the following JavaScript programming concepts to comprehend this example:

  • Objects in JavaScript
  • Constructor function in JavaScript

An object can be made in one of three ways:

  • The use of object literal
  • By explicitly constructing an Object instance
  • Making use of the constructor function

Example 1: The use of object literal

// a programmer that uses object literals to construct JavaScript objects
const person = { 
    name: 'Shailesh',
    age: 20,
    hobbies: ['reading', 'cricket', 'coder'],
    common: function() {
        console.log('Hello everyone.');
    },
    score: {
        maths: 90,
        marathi: 80
    }
};

console.log(typeof person);
console.log(person.name);
console.log(person.hobbies[0]);
person.common();
console.log(person.score.maths);

Output


object
Shailesh
reading
Hello everyone.
90

We have generated an object called person in this code.

An object literal can be used to build an object. To generate an item directly, an object literal employs the symbol.

A key:value pair creates an object.

Functions, arrays, and even objects can all be defined inside of another object. Using dot notation, you can get at the object's value.

The following syntax is used to create an object using an object instance:

const objectName = new Object();

Example 2: By explicitly constructing an object instance

// a programmer that uses object literals to construct JavaScript objects
const person = new Object ( { 
    name: 'Shailesh',
    age: 20,
    hobbies: ['reading', 'cricket', 'coding'],
    common: function() {
        console.log('Hello everyone.');
    },
    score: {
        maths: 90,
        science: 80
    }
});

console.log(typeof person); 
console.log(person.name);
console.log(person.hobbies[0]);
person.common();
console.log(person.score.maths);

Here, an object is created by using the new keyword together with the Object() instance.

Example 3: Create an object using Constructor Function

// program to create JavaScript object using instance of an object

function Person() {
    this.name = 'Shailesh',
    this.age = 20,
    this.hobbies = ['reading', 'cricket', 'coding'],
    this.common= function() {
        console.log('Hello everyone.');
    },
    this.score = {
        maths: 90,
        science: 80
    }

}

const person = new Person();

console.log(typeof person); // object

// accessing the object value
console.log(person.name);
console.log(person.hobbies[0]);
person.common();
console.log(person.score.maths);

The new keyword is used in the example above to create an object using the Person() Constructor function.

An object is made by calling new Person().


Recommended Posts

View All

JavaScript Immediately-invoked Function Expressions (IIFE)


Learn about Immediately-invoked Function Expressions (IIFE) in JavaScript. Understand their benefits and how to use them effectively. Start coding now

Learn JavaScript Closures with Code Examples


Master JavaScript closures with code examples! Our comprehensive guide explains how closures work and how to use them in your code. Start learning now...

4 Ways of Passive Income for a Programmer


Everyone wants to make extra money, but owing to time limits and jobs, we can't devote more time to it.. Find out how to become a programmer who makes...

Top 10 ES6 Features Every JavaScript Developer Must Know


The specification for JavaScript implementation is provided by ES6. Learn about its recently added ES6 features that make writing JavaScript easier!

What is the let keyword in JavaScript?


Learn all about the let keyword in JavaScript and how it differs from var. Explore its scope, hoisting, and best practices for usage.