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 trial

JavaScript Object-Oriented JavaScript Working with Classes in JavaScript Instantiating a Pet Object

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

Concatenating 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
Steven Parker
231,184 Points

If 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]".

Steven Parker
Steven Parker
231,184 Points

You could create your own "toString" method, perhaps something like this:

class Pet {
  constructor(animal, age, type) {
    this.animal = animal;
    this.age = age;
    this.type = type;
  }
  toString() {  // this will be done anytime the object is converted to a string
    return `Pet { animal: '${this.animal}', age: ${this.age}, type: '${this.type}', sayWhat: ${this.sayWhat} }`;
  }
}
Steven Parker
Steven Parker
231,184 Points

You 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

So 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
Steven Parker
231,184 Points

You'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.

Steven 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