• How To Display [Object Object] In console.log or Alert in JavaScript (LWC & Lightning Aura Components)

    Published By: Venu Gutta
    Published on: Saturday, 1 August 2020
    A- A+

    Display [Object Object] As String In JavaScript: 

    Display 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" };
  • 1 comment to ''How To Display [Object Object] In console.log or Alert in JavaScript (LWC & Lightning Aura Components)"

    ADD COMMENT