Javascript Data Type Question Convert to String

Introduction

What is the output of the following code?

let myObj = {a:true, b:23};
let myString = "The object: " + myObj;
console.log(myString);


The object: [object Object]

// Convert a generic object to a string

// First we create our object
let myObj = {a:true, b:23};

// The following statement will trigger an implicit
// conversion of the object to a string
let myString = "The object: " + myObj;

// Now we write the result out to the page
console.log(myString);//from   ww  w.  ja v a  2s . com
// "The object: [object Object]"



PreviousNext

Related