Introduction
In this article, we will learn about objects in JavaScript. Everything in JavaScript is an object. In addition, JavaScript allows us to define our own objects. So, when we code in JavaScript we can call each and every single token an object.
- Arrays are always objects, even functions are always objects
- Dates are always objects
- Booleans can be objects or primitive data treated as objects
- Numbers can be objects or primitive data treated as objects
- Strings are also objects or primitive data treated as objects
- Maths and RegularExpressions are always objects
Let's see how to define an object and set a property on that.
Open the Notepad++ editor and write the following code:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var obj = new Object();
- obj.name = "csharp";
- obj.lastname = "corner";
- alert("Name:- " + obj.name + " Lastname:- " + obj.lastname);
- </script>
- </form>
- </body>
- </html>
In JavaScript, we can create an object and set a property on fly. Here is a sample output of the program above.

Now let's see how to initialize the object using constructor. Have a look at the following code.



Join the conversation! Your thoughts help the community grow.