Methods to create Objects in JavaScript

In JavaScript 'Object' is a data type which is used to store data in the form of key and values.
So, there are four methods to create objects:
1) Object literals
2) Function constructor
3) Using ES6 class
4) By using Object.create() method
I want to tell you that I am not explaining fourth method because I have not understood it properly. If anyone knows simple explanation of fourth method, please suggest.

1) Object literals

In this method we create objects as follows:

let student1 = {
name: "John",
rollNo: 25,
email: 'john25@gmail.com'
}
let student2 = {
name: "Riya",
rollNo: 23,
email: 'riya23@gmail.com'
}

In this method we create objects and write keys and values for each object separately. So, we can see both above objects have same keys but have different values. If we want to create 1000 objects so we have to write 5000 lines. So are you ready to write 5000 lines...
OfCourse not so there are other methods which will help to create objects in few lines of code. Let's see second method.

2) Function Constructor

Syntax:

function person(name, age, country){ // "Radha",25,"India"
this.name = name //p1.name = "Radha"
this.age = age       //p1.age = 25
this.country= country//p1.country= "India"
}

So, let's create object from above function constructor.

let p1 = new person("Radha",25,"India")

In above example, p1 is object that we have created from constructor 'person'. "Radha" will go to name, 25 will give go to age, "India" will go to country. 'this' => current object so, our current object is p1. so the values will set for the keys or properties that are defined in 'person'.

p1.name = "Radha"
p1.age = 25
p1.country= "India"

Try to code and run the program you will understand perfectly.
By using above constructor, we can create as many objects as we want in just one line.

let p2 = new person("Julie",23,"America")
let p3 = new person("Amy",21,"Australia")

We can add new property, update value of existing property for a particular object separately as we make changes in normal object. It will not change other objects or function constructor. Now let's see third method.

3) ES6 class

First of all, we should know definition of class.
Class: Class is a template to create object.
For example, Mould for making idols.
So here by using mould we can make as many idols as we want.
mould=> class
idols=> objects
Remember mould is not idol.
So, we use class to create objects but class is not a object.

Syntax:

class class_name{
constructor(a, b) {
this.name = a
this.city = b
}
}

So, let's create a class called Student.

class Student{
constructor(fn,ln,ag){
this.firstName = fn
this.lastName = ln
this.age = ag
}
}

Now create objects from class Student.

let s1 = new Student("John","Doe",40)

Here also same "John" will go to fn ,"Doe" will go to ln, 40 will go to ag.
Now in constructor below things will happen.
this=> s1
s1.firstName = "John"
s1.lastName = "Doe"
s1.age = 40

let's create another object from Student class

let s2 = new Student("Kia","Wolf",30)

Same things as above object but this time this=> s2
Now in constructor below things will happen.

s2.firstName = "Kia"
s2.lastName = "Wolf"
s2.age = 40

Thats it.