
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to add a property to a JavaScript object using a variable as the name?
In this article, you will understand how to add a property to a JavaScript object using a variable as the name. Adding property to an object can be achieved by two methods. The first is the dot (.) notation and the second is using the brackets([]).
Example 1
In this example, let’s use the dot (.) notation.
var inputObject = {a: "value1"}; console.log("An object is created with properties: ", inputObject) inputObject.b = "value2"; console.log("
After adding properties, the object now contains: ", inputObject) console.log(inputObject)
Explanation
Step 1 − Define an object namely inputObject.
Step 2 − Add an additional property to the object using dot notation.
Step 3 − Display the values.
Example 2
In this example,
var inputObject = {a: "value1"}; console.log("An object is created with properties: ", inputObject) inputObject['c'] = "value3" console.log("
After adding properties, the object now contains: ", inputObject) console.log(inputObject)
Explanation
Step 1 − Define an object namely inputObject.
Step 2 − Add an additional property to the object using bracket notation.
Step 3 − Display the values.
- Related Articles
- Add a property to a JavaScript object constructor?
- How to ignore using variable name as a literal while using push() in JavaScript?
- How to load a JavaScript function using the variable name?
- How to create an object property from a variable value in JavaScript?
- How to execute a JavaScript function using its name in a variable?
- How to add an element to a JSON object using JavaScript?
- How to conditionally add a member to an object using JavaScript?
- How to add a method to a JavaScript object?
- How to add a property, method to a JavaScript constructor?
- How to get a variable name as a string in Python?
- How to get a variable name as a string in PHP?
- How to remove a property from a JavaScript object?
- How to add an element to a javascript object?
- How to use a variable for a key in a JavaScript object literal?
- Add a method to a JavaScript object constructor?

Advertisements