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 Adding Methods To Our Class

I don't quite understand why do we need to use the method here in the class syntax?

class Pet { constructor(animal, age, breed, sound) { this.animal = animal; this.age = age; this.breed = breed; this.sound = sound; }

speak() { console.log(this.sound); }

class Student { constructor(gpa){ this.gpa = gpa; } stringGPA() { return this.gpa = "gpa"; } } const student = new Student (3.9);

What happens if we don't use the method?

Blake Larson
Blake Larson
13,014 Points

Are you asking why we use the new Student(3.9) syntax?

1 Answer

Mark Casavantes
Mark Casavantes
2,880 Points

Hello Hanwen,

Functions or methods are used to break your code into smaller parts. When you write a large program you can work on one method at a time to make sure it works properly.

If you do not use methods, you just have one program that does everything.

I hope this is helpful to you.