How to create object properties in JavaScript?

JavaScript object properties are the keys inside the body of a JavaScript object. Object properties can be any of the three primitive data types, or any of the abstract data types, such as another object. Object properties are usually variables used internally in the object’s methods, but can also be globally visible variables that are used throughout the page. These properties can either be statically typed by the programmer at the time of object declaration, or they can be modified or created later according to the need.

There are two ways of creating or adding properties to a JavaScript object −

  • Using Dot Notation
  • Using Square Brackets Notation

Let us study them one by one with code examples.

Using Dot Notation

You can add or create JavaScript object properties by using Dot syntax. To use dot syntax, use dot between the new property or key name and the existing object’s name and assign a value to it by assignment operator as shown below −

Syntax

Following is the syntax for creating or adding new properties to a JavaScript object using Dot Syntax −

objectName.propertyName = propertyValue;

In above syntax, objectName is an already declared JavaScript object to which we are adding new property or key named as propertyName, while on the right hand side of the assignment operator the propertyValue is the value that has to be assigned to that particular newly created or added property or key.

Algorithm

  • Step 1 − As the first step of creating or adding new properties to a JavaScript object you need to create a JavaScript object according to the syntax discussed above.
  • Step 2 − In the next step, you need to add or create a new JavaScript object property or key by using the Dot Syntax as discussed above.
  • Step 3 − In the third or the last step of the process, you need to display the object on the screen with the newly added or created property to it.

Example 1

The below example will illustrate the use of dot syntax for creating or adding new properties to already existing JavaScript object with several properties −

<!DOCTYPE html>
<html>
<body>
   <h3>Creating new object property using Dot Notation.</h3>
   <p>Create or add a new property named "title" to the existing object "obj"</p>
   <p id="result">Resultant object:<br></p>
   <script>
      let obj = {
         company: "FirstEnquiry",
         article: 1
      };
      obj.title = "How to create object properties in JavaScript?";
      console.log(obj);
      document.getElementById("result").innerHTML +=
      JSON.stringify(obj);
   </script>
</body>
</html>

Using Square Brackets Notation

It is another way of creating or adding properties to a JavaScript object. In this notation, the name of the object is followed by the square braces with the property name passed as a string inside them and the value will be assigned using the assignment operator as assigned in the dot notation.

Syntax

Following is the syntax for adding or creating new properties of a JavaScript object using Square Brackets Syntax −

objectName[“propertyName”] = propertyValue;

In above syntax, objectName is an already existing JavaScript object whose properties are increased with one by adding a new property named propertyName inside square brackets and double quotes, while on the right hand side of assignment operator propertyValue is the actual value that will be assigned to the newly created or added property of object.

Algorithm

  • Step 1 − In first step, you will create a JavaScript object with several properties to which you will add properties later.
  • Step 2 − The second step of the process involves the adding or creating the JavaScript object property using the square brackets syntax.
  • Step 3 − The last or the third step will display the object on the user screen. So that user can clearly see the change in the properties of objects after and before adding new properties.

Example 2

Below example will illustrates the use of square brackets syntax for creating or adding new properties to already existing JavaScript object with several properties −

<html>
<body>
   <h3>Creating new object property using square brackets Notation.</h3>
   <p>Create or add a new property named "title" to the existing object "obj".</p>
   <p id="result">Resultant object:<br></p>
   <script>
      let obj = {
         company: "FirstEnquiry",
         article: 1
      };
      obj["title"] = "How to create object properties in JavaScript?";
      console.log(obj);
      document.getElementById("result").innerHTML +=
      JSON.stringify(obj);
   </script>
</body>
</html>

Conclusion

The Dot Notation out of two notations discussed above is a more convenient and preferred way to add or create new properties to a JavaScript object as it is simple and easy to use as compared to Square Brackets Notation.

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…