How to JSON Add an Element in Javascript?

Nov 02, 2022 . Admin

Hello friends,

In this example, you will learn how to JSON add an element in javascript. it's a simple example of adding a new array element to a JSON object. I explained simply how to add an element to a JSON object with javascript. This article goes in detailed on how to add key-value pair in the JSON object.

In order to add Key/value pair to a JSON object, Either we use dot notation or square bracket notation. Both methods are widely accepted. This example adds {“prop_4”: “val_4”} to the GFG_p object by using dot notation.

So, let's see bellow solution:

<!DOCTYPE html>
        How to JSON Add an Element in Javascript?
<body style="text-align:center;">
    <h1 style="color:#006AFF;">  
           How to JSON Add an Element in Javascript?
    <p id="GFG_up"
       style=" font-weight: bold">
    <button onclick="java()">
        Click to add
    <p id="GFG_down" 
              font-weight: bold" ;>
        var GFG_p = {
            prop_1: "val_1",
            prop_2: "val_2",
            prop_3: "val_3"
        var p_up = 
        var p_down = 
        p_up.innerHTML = 
        function java() {
            GFG_p.prop_4 = "val_4";
            p_down.innerHTML = JSON.stringify(GFG_p);

I hope it will help you...