How To Create Object In Javascript


Create Object In Javascript

Here We Discuss About How to Create Object In Javascript. There are some ways during which objects in JavaScript differ from objects in other mainstream programming languages, like Java. i will be able to attempt to cover that during a another topic. Here, allow us to only specialise in the varied ways during which JavaScript allows us to make objects.

How To Create Object In Javascript

In JavaScript, consider objects as a set of ‘key: value’ pairs. This brings to us the primary and hottest way we create objects in JavaScript.

The article shall cover the subsequent topics:

  1. Object creation using String Literal
  2. Creating Objects using "New Object"
  3. Using "Constructor Function" to make Objects


    Object Literal is that the simplest sort of creating Objects in JavaScript.It is a group of comma seperated values enclosed inside "{ }"It don't require "new" keyword to be initialised.

    var Employee = {
      name: 'Mayank',
      age: '28,
      designation: 'Engineer'

    This is a Simple Object.

    Accessing Properties of Object Literal

    2. Employee['age']

    Adding Properties to Object Literals:

    We can add further properties to the thing Literal Notation.

    Employee.salary = "1LPA";
    Employee.Address = "Rohini";

    Adding Methods to Object Literal

    Apart from properties, we will add Methods to Object Literals

    Employee.salary = "1LPA";
    Employee.Address = "Rohini";

    Also we will add function later once the thing Literal is defined.

    Employee.getDetails = function() {
      // Some Code

    Advantages of Object Literal:

    1. No got to invoke constructors directly
    2. No got to pass Arguments to the constructor function
    3. It creates a singleton Object
    4. It executes the fastest to make an Object


    The Other thanks to create object are to make them using "new Object".

    Lets search for the straightforward implementation of this method:

    var Employee = new Object(); = "Mayank";
    Employee.age = 28;
    Employee.showDetails = function() {
      // Some Code

    The above Or Before Used method using Öbject Literal" and this behaves within a similar manner.The outcome of both the methods are same.Object Literal is comparitively easier method to make an Object.

    Every Object springs from "Öbject" in JavaScript.

    var Employee = new Object();
    typeof Employee;    output: "object"

    Similarly even Object literals would create a kind of "Object"

    var Employee = { };
    typeof Employee;    // output: "object"

    Both the implementation shall end in the variable of type Object


    Constructor function is an efficient thanks to Create Objects within the following casses:

    1. Create a building block from which other Object are often created
    2. Design a basic architecture to make more Objects

    What is a Constructor Function

    function Employee (name, age, designation) {
      this.Name = name;
      this.Age = age;
      this.Designation = designation;

    This is an easy Implementation of the Consructor Function.

    Constructor function acts Like a building block to store the detail of any Employee. All Of  the variables added to "this" keyword shall act Like a public member to the created Object. For those that belong to language like Java and C#, a constructor function shall resembel to a "Class".We can create Objects from Constructor function even as we create Objects from Classes.

    We also even add methods to the constructor function:

    function Employee (name, age, designation) {
      this.Name = name;
      this.Age = age;
      this.Designation = designation;
      this.showDetails = function() [
        console.log("Age: " + this.Age + " Name: " + this.Name);

    The added function are going to be available to every of the thing created.


    Lets create a Object from a Constructor Function.

    var newEmployee = new Employee("Mayank", 28, "Senior Software Developer"); 

    We can create multiple instances of Employee using this "new" keyword.

    We can now access the properties of the New Object within the following manner

    1. newEmployee.Name
    2. newEmployee['Age']

    Lets see what "New Keyword" does while creating a replacement Object.

    • Create a replacement scope within the memory
    • Bind variables to the new scope
    • ask the newly created scope by "this"
    • Return "this" Object from the constructor function.
    You Can Use Any Method To Create Object In Javascript.Its Depends On Your Projects

    Post a Comment

    Post a Comment