This is an example program that shows the difference between the stringify() and toString() functions in JavaScript.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>XoaX.net's Javascript</title> <!-- Suppress favicon issues --> <link rel="icon" href="data:,"> <script type="text/javascript" src="StringifyVersusToString.js"></script> </head> <body onload="Initialize()"> <h1>Stringify()</h1> <pre id="idObject1"></pre> <h2>versus</h2> <h1>toString()</h1> <pre id="idObject2"></pre> </body> </html>
function Initialize() {
let qObject = {
msName: "Jesus Christ",
mqBirthday: new Date("0000-12-25"),
mdAge: 30.5,
mbIsGod: new Boolean(1),
mbIsMan: true,
mqBiologicalFather: null,
msaApostles: ["Peter", "John", "James", "Thomas"],
mfnMultiply: function(x) { return 100000*x;},
mqParents: {sFather: "Joseph", sMother: "Mary"}
};
let qElement = document.getElementById("idObject1");
// This function argument needs to convert functions to strings. Otherwise, functions will be skipped.
let sJson = JSON.stringify(qObject, (key, value) => {
if (typeof value === "function") {
return value.toString();
}
return value;
}, '\t');
qElement.innerHTML = sJson;
document.getElementById("idObject2").innerHTML = qObject.toString();
}© 20072026 XoaX.net LLC. All rights reserved.