Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 PointsConcatenating object and string
Please refer to the code comments
class Pet {
constructor(animal, age, type) {
this.animal = animal;
this.age = age;
this.type = type;
}
}
const dogA = new Pet('dog', 3, 'rottweiler');
const catB = new Pet('cat', 1, 'cat?');
console.log(dogA + "abc");
//If I run dogA by itself, it returns the new object,
//but as soon as I add the "abc" string,
//in the console I receive [object Object]abc Why is that?
console.log(catB);
4 Answers
Steven Parker
231,236 PointsIf you log an object, the system lists out the object properties for you.
But if you concatenate the object with a string, it must first convert the object itself to a string and the default for the conversion is "[object Object]".
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 PointsThanks, so how would you go about and combining an object and a string?
Steven Parker
231,236 PointsYou might create a "toString" method in the object, but it depends on what kind of result you are trying to achieve.
Can you give an example of an object, the operation you wish to perform on it, and the result you would expect to see?
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 PointsSo something like this:
const dogA = new Pet('dog', 3, 'rottweiler', "woof");
const catB = new Pet('cat', 1, 'cat?', "miaw");
const dogB = dogA.toString();
console.log(dogB + " test");
It still prints Object object. I am just trying to experiment to see, if I were to add a string and an object together how that would be. So that is
Pet { animal: 'cat', age: 1, type: 'cat?', sayWhat: 'miaw' } test
Steven Parker
231,236 PointsYou're calling the default "toString", which is what the system does anyway. What I meant was to create your own conversion method. See the comment I added to my answer for an example.
Jason Larson
8,361 PointsSteven is right in that you can write your own .toString method, but if you're working with other code and not writing your own class, the easiest way to print out the properties of an object (including concatenating strings to them) is to use the JSON library's stringify() method. So, using your example:
const dogA = new Pet('dog', 3, 'rottweiler', "woof");
const catB = new Pet('cat', 1, 'cat?', "miaw");
console.log(JSON.stringify(catB) + " test");
//The above code would return:
{"animal":"cat","age":1,"type":"cat?","sayWhat":"miaw"} test
Steven Parker
231,236 PointsSteven Parker
231,236 PointsYou could create your own "toString" method, perhaps something like this: