"Java Data Structures - Retired" was retired on May 31, 2019. You are now viewing the recommended replacement.

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

Confused about Methods in Javascript

Hi,

Please bare in mind that I'm a complete newbie, but just want to understand some terminology differences. i'm a little bit confused about the term Methods in Javascript.

For example the toUpperCase() is called a method:

var str = "Hello World!"; var res = str.toUpperCase();

.. and then parseFloat('3.14') is also called a method.

What confuses me is that in the first case the syntax starts with the object 'str' and then .toUpperCase(), while in the parseFloat example you start with parseFloat and then the "object" inside the parenthesis.

Anyone have an explanation for this?

Cheers!

I hope this helps,

toUpperCase() is a method on a object i.e. it returns something about that object. parseFloat takes your object & returns something else.

2 Answers

parseFloat is really a function, it is not called on an object like a method but the terms are sometimes used interchangeably. functions are more 'high-level' in that they are not part of any class, whereas methods are part of some class. so methods are functions - functions of a class. methods act on objects, it's an object-oriented programming term. functions take arguments and return values generally, no object needed.

Thanks a lot guys. It's starting to make more sense to me now.

So the way I think of it now is that string is a class in Javascript, and it has some "built-in" methods that can be applied to it, for example toUpperCase().

On the other hand parseFloat() is a "built-in" function in Javascript, and not connected in any way to any particular class. And the argument that's being passed through the function needs to start with a number for it to return a value.

That is it.