Skip to content

Add property to object JavaScript | Example code

  • by

Use the dot notation to Add property to object in JavaScript. Below code added the foo property to the obj object above with value 1. = 1;

You can also add a property by using the bracket notation:

obj['foo'] = 1;

Both ways do the same but bracket nation can have invalid property identifiers in the string.

Add property to object JavaScript

Simple example code.

<!DOCTYPE html>


    var obj = { Name: "Joe" };

    obj.Age = 25;

    obj['Country'] =  "USA"




Add property to object JavaScript

Is it possible to add dynamically named properties to JavaScript objects?

Answer: Yes, it is possible, see below code.

var data = {
    'PropertyA': 1,
    'PropertyB': 2,
    'PropertyC': 3

data["PropertyD"] = 4;

// dialog box with 4 in it

Do comment if you have any doubts or suggestions on this JS object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.