How do I display the content of a JavaScript object in a string format?

Use document.getElementById() and display using innerHTML. Following is the code −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<p id="objectId"></p>
   var jsonObject="name";
   var value = jsonObject.split(" ");
   var add = {};
      add[v] ? add[v]++ : add[v] = "John";
   document.getElementById("objectId").innerHTML = JSON.stringify(add);

To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.


This will produce the following output −