Display [Object Object] As String In JavaScript:
In JavaScript if we trying to print other than primitive type variables in console.log or alert, then it will returns [Object Object]. If its return output as [Object Object], we can't know output as expected or not.
By using JSON.stringify() function we can print output in string format. JSON.stringify() is a predefined JavaScript function, it takes object as a input parameter and returns json string if object in JavaScript specific object structure.
Example: Display Object as String in js
var cObj = { name: "Salesforec", age: 10, city: "CA" };
console.log("cObj===>"+cObj ); //output : cObj===>"+[Object Object]
console.log("cObj in String Format===>"+JSON.stringify(cObj) );
// output: cObj in String Format===> { name: "Salesforec", age: 10, city: "CA" };
excellent
ReplyDelete