Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
210,066 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
210,066 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
210,066 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
210,066 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.

Jason Larson
Jason Larson
4,896 Points

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