
Skip to main content
How can I add a key/value pair to a JavaScript object
You can add key/value pair to a javascript object using dot notation and square brackets notation.
-
Add key/value pair to javascript object using dot notation
const obj = { key1: 'value1', key2: 'value2' }; obj.key3 = "value3"; console.log(obj);
Output:
{ key1: 'value1', key2: 'value2', key3: 'value3' }
You have to use dot notation in between object and new key (object.key) and assign value to key using assignment operator (=) in javascript to add a new key and value pair to an existing javascript object.
-
Add key/value pair to javascript object usign square bracket notation
const obj = { key1: 'value1', key2: 'value2' }; obj["key3"] = "value3"; console.log(obj);
Output:
{ key1: 'value1', key2: 'value2', key3: 'value3' }
You can add a new key/value pair to object in javascript using square ([]) notation. It will helps you to add a new values (key/value) to an existing object in javascript.
Bootstrap
Cmd
CodeIgniter
Css
Drupal
Git
Html
Javascript
Jquery
Laravel
Nodejs
Php
Python
Sql
Typescript
Wordpress
Random Code Snippet Queries: Javascript
- How to set href value of anchor tag in javascript
- How to add key/value pair to existing form data
- How to display an image in Javascript onclick
- Get hostname from URL in javascript
- Multidimensional array example javascript
- How to display for slash as a string in javascript HTML
- Deault order of record in datatable
- Javascript get only 10 characters from string
- Animating numbers counting Javascript
- How to crop multiple images with cropper js
- Javascript to print Fibonacci series upto 15 Observations
- How to get file extension using javascript/jquery
- How to show and hide placeholder text
- How to check file is an image in javascript using filename
- Hide div on click outside of element in javascript/jQuery
- How to Set data to localstorage in javascript
- JavaScript test() Method
- How to get data from localstorage in javascript
- Uncaught TypeError: Cannot read property 'addEventListener' of undefined
- Execute function with condition in javascript
- How to get query string value in javascript
- How to preview a word document in HTML using javascript
- Uncaught SyntaxError: Identifier 'CODE' has already been declared
- Prism js horizontally overflowing its container
- Get multiplication in javascript using array input