In JavaScript, there are five different ways to create objects. We have already talked about objects, and how to create them by the simple method, which is just creating a variable and then the curly braces notation. But there are five different ways. So let's get started. The first method is using the new Object Notation, which as you can see on the screen, you can just create a VAR person, and then you just type equals new object. And then you start adding properties to that object.
So the first method, you just create a variable var person equals to new and then object with a capital O, and then parentheses and then semicolon. And then you just use the dot notation to add properties. So you just I person And then you add properties. For example, you want to add v name equals to john, and then semicolon. And then for example, you want to add another property age, person dot age equals to 25 and the semicolon and then console dot log. And then you just pass the person first, and save and run.
And as you can see, we get this object. So this is the first method by using the new and then object keyword. The second method we talked about before, which is just creating a simple variable, and then equals to, and then curly braces. And inside these curly braces, you add the properties that you want to have for this object. So for example, a person might have a name. So you add the name, and then call and then colon and then the property.
Value, which is, of course, a name, which is e or K, and then comma, and then the other property, which could be h, and then colon and then the value. So let's take an example. So I'm going to just comment these out here. And then create the second method, which is var person and then equals to just add curly braces. And inside these curly braces, you add the properties. So the first one name, john, and then comma, and then H, and then the value 25.
And just semicolon and you can print it. I'm going to use the console log again here and copy and paste it and save and run and we get the same exact result as before. So this is the second method on how to create an object by using Just a simple variable and then you add curly braces and add properties inside the third method is by using a function and then add the this keyword and then you pass variables to this function or just add values to this function inside. And this function is called also known as constructor function. So you just create a function and then pass the properties. For example, we want to have for a person a name and age as properties.
So we pass them and use the the this keyword and then we assign that variable that value that we have passed to the name and then the second value or parameter that we have passed to the second property which is age. And then you create a new object from That function using new keyword. So the third method is a constructor function. Usually you just create a function function, and then the name of that function, which is the name of the object, which is going to be person. And usually when using our constructor function, we make the first letter capital. So here person with a capital letter for the P, and then parentheses, and then inside them, you add the properties that you want to pass, I want to pass the name as well the age and then curly braces.
And inside these curly braces, the most important part for constructor functions, which is this keyword, race, you just type this and what this means is that it refers to this object, meaning that this object, this object, which is Pearson, Has you should you just add dot and the name as a name, and in equals to the value that you are going to pass to this person, which is this name that you passed here, and then semicolon. And then you are the second value or second parameter, you pass it here age, and then you again use this keyword and then dot age. And again, this race this keyword means that this object has a property called age and its value equals to this age that we just passed. And then semicolon and then you just create a new object from this function, just type new and then person and make sure that this this person exists.
Actually matches this one. And it has, it's better to have E capital because it's conventional to use p capital for constructor functions, and then semicolon. And here you just pass the values that you want to have for this for for this object. So I want this to have Kate as a name, and the age is 25. And we need to store this object in a variable for and we can name this variable person one equals to this value, and we can use console dot log, and we just pass this person one. And if I save and run, I am going to get the same exact result as before.
We get the name as here the name ASCII and the value, the age value 25 and the past of using objects is that you can create as many objects as you can. So for example, I can create another person for person to, and then equals to new, and then person. And then you just pass the other value, I want this person to have any mom, john, and age of 23, and then semicolon and they can also use console dot log. And they just ask person two. And if I save and run, I am going to get person two. And as you can see, I can get as many variations of this object as I can.
So this is the third method, which is called the constructor method. The fourth method is by using the object class and then calling the Create a method given by JavaScript. And in that case, you don't have to use it. constructor to create a constructor, you just create your simple object. And then you create as many variations as you can by creating variables and then you just call the Create method on the object class, and change the variable each time you create a new object by creating a new variable, and just changing the name of that variable. So let's take an example.
So to create an example for this method, for this fourth method, you just create a simple variable for example, cold person, and then it's going to be equal to object. And inside that object, I'm going to give just default values so the default values are name and then eight for example, and age. Age 23 23 and then what they would do is just use the object class and they call the Create function here create and then pass that person. And then I just store the result inside a simple variable called var person one, person one equals to this. And then I use the console log console dot log, and I pass person one. And I can call the default value for example me.
And if I save and run, I am going to get the kit if I change this to age, I am going to get age 23. And these are the default. Of course, they can change that by using person one dot name equals Alice Alice If I save and change this to name, and Save and Run, I get Alice. As you can see, I was able to change the default value. And they can also change, I can also create other variations. So I can create a new object here, where person two equals to object, dot create.
And then person. And if I pass here, instead of person one, person two, if I save and run, I am gonna get the default value, which is Kate. And of course, I can change it as we did here before. But remember that this method is used less than the constructor method, because the constructor method is much better and much, much conventional. So this method is rarely used. But the but this method is useful in case you don't want to create a constructor method.
It's going to get the job done for you. And you don't have to create a constructor method as we did here. But I wouldn't recommend using this method, I would recommend, I would recommend going with the flow because many programmers like to use our constructors, not enjoy in just JavaScript in other programming languages. So this method is good, but it's not being used as much as the constructor method.