
- 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 ignore using variable name as a literal while using push() in JavaScript?
To avoid using variable name as a literal, use square brackets. Following is the code −
Example
var name = "David" var putTheAllData = [] putTheAllData.push( { name: "The name is name will remain same" } ) putTheAllData.push( { [name]: "The name is David will be changed [name]"} ) console.log(putTheAllData);
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo166.js. This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo166.js [ { name: 'The name is name will remain same' }, { David: 'The name is David will be changed [name]' } ]
- Related Articles
- How to add a property to a JavaScript object using a variable as the name?
- How to load a JavaScript function using the variable name?
- How to execute a JavaScript function using its name in a variable?
- Using predefined class name as Class or Variable name in Java
- How to display a variable as tooltip in ggplotly using R language?
- MySQL syntax error (in SELECT query) while using ‘group’ as table name
- How to reverse a string using only one variable in JavaScript
- Using DSN name while using a remote source in SAP HANA
- How to get a variable name as a string in Python?
- How to get a variable name as a string in PHP?
- How to show a while loop using a flow chart in JavaScript?
- How to resolve the error that occurs while using a reserved word as a table or column name in MySQL?
- How to ignore hidden files using os.listdir() in Python?
- How to use quotes correctly while using LIKE with search variable in MySQL in Java?
- How to ignore a class during the serialization using Jackson in Java?

Advertisements