Add Key Value to Object in JavaScript17 Mar 2025 | 3 min read We will understand how to add key value to object in JavaScript. Let us first understand the key-value pairs. Key-Value PairsThe key-value pairs are a basic way to store data. The key is the unique identifier and the value is the data related to that key. These pairs are helpful for organizing data inside JavaScript objects and arrays. Creating and accessing Key-Value PairsIt is simple to create key-value pairs in JavaScript. For Objects, we have to define keys and give them values related to the keys. For arrays, every element present in the array has an index which is considered the key with the related value. Constructing and accessing key-value pairs in an objectCode: Output: Here in the output we can witness key-value pairs in an object. ![]() Constructing and accessing key-value pairs in an arrayCode: Output: Here in the outcome we can witness key-value pairs in an array. ![]() There are various ways to add key-value pair to an Object which are given below:
Let us understand these ways one by one. Utilizing Dot NotationWe can add key-value pair to an object with the help of dot notation. The key must be a symbol or a String. Demo:We will utilize the dot notation for adding the key-value pair to an object. Code: Output: We can witness output downwards in which the key-value pair is added utilizing the dot notation. ![]() Utilizing Square Bracket NotationThe dot notation has some drawbacks such as it cannot handle the dynamic key due to which error occurs. The square bracket notation comes into account to remove this drawback. Demo:We will utilize the square bracket notation to add a key-value pair. Code: Output: We can witness output downwards in which the key-value pair is added utilizing the square bracket notation. ![]() Utilizing spread (…) SyntaxIt allows to spread iterable such as an array, object or string. Demo-1:We will utilize the spread (…) syntax to add a key-value pair. It is utilized to create a new object. Code: Output:
![]() Demo-2:It allows us to add multiple properties simultaneously but if the same property already exists on the Object then the value of the property will be overwritten and a new value will be given to the property. Code: Output: We can witness that the value of the key "age" was 40, but it has been overwritten, and the value of the key "age" becomes 45. ![]() Utilizing Object.assign()The Object.assign() method is kind of the same as that of spread (…) syntax. Demo:We will utilize the Object.assign() method to add a key-value pair to an object. Code: Output: Here in the outcome downwards we can witness the key-value pair created utilizing the Object.assign() method. ![]() Utilizing Object.defineProperty()The Object.defineProperty() is utilized to modify the property of an object. Demo:We will utilize the Object.defineProperty() to add a key-value pair. The season will not be overwritten because we have utilized writable: false. Code: Output: Here in the outcome downwards we can witness the key-value pair constructed utilizing the Object.defineProperty() method. ![]() Conclusion:We have understood how to add key value to object JavaScript. The key-value pairs are utilized to organize data in a proper way so it can be easily accessible. We can add key values utilizing various ways such as Dot Notation, Square Bracket Notation, spread (…) Syntax, Object.assign() and Object.defineProperty(). Next TopicJavascript-vs-cpp |
In web development, interactivity plays a pivotal role in creating engaging and dynamic user experiences. JavaScript, being a versatile scripting language, offers a multitude of events to facilitate interaction with web elements. One such event is onfocus. In this article, we'll delve into what the onfocus...
7 min read
? A necessary part of strong programming advancement is the means by which to handle errors. Throwing errors is a way to handle and respond to unexpected conditions in your JavaScript code. Coders who know how to throw errors can foster code that is simpler to use...
5 min read
What is ? Zod is a JavaScript and typescript library that helps developers to define the schema for their data. Schema is a blueprint that describes the shape and constraints of data in this context. Zod helps the developers to define these schemas concisely and then use...
6 min read
Sorting the arrays of objects in JavaScript is a basic manipulation and is also a really important part of programming which is used in day-to-day web development. JavaScript offers two built-in sorting methods: Array.prototype.sort and Array. Sort that both sort the elements in place, but the...
6 min read
JavaScript string includes() The JavaScript includes() method is used to check if a specified substring is present in a given string. It is a case-sensitive method. It returns the Boolean value, either true or false. It returns true if the string contains the specified substring and...
2 min read
Both JavaScript and C++ are alent programming languages. JavaScript is a scripting language utilized for web designing whereas C++ is a general-purpose language utilized for low-level system programming. In this article, we will comprehend the difference between JavaScript and C++. Before that, let us understand about...
5 min read
We will understand the JavaScript URL encode in this article. URL encoding is the procedure of converting characters within a URL into a format that can be transmitted over the internet. URL decoding is the procedure of converting the encoded URL back to its original form. It...
3 min read
JavaScript is a scripting language that enables developers to write interactive web applications that run in a web browser. A very common operation one would perform with JavaScript is subtracting two dates. This report will discuss the rationale for subtracting dates, the approaches to performing this...
5 min read
Introduction to D3.js Data-Driven Documents or D3.js is the package available in JavaScript which is created by Mike Bostock for the visualization process using CSS, HTML, SVG, etc. This is the most useful, adaptable, and very strong visualization tool for complex data. The D3.js library is mainly...
15 min read
When it comes to web development, JavaScript is one of the most popular and adaptable programming languages. Because of its adaptability, it is a fundamental component of contemporary web development, enabling developers to create dynamic and interactive web pages. The ability to extract meaning from code...
9 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India