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 JavaScript Functions Pass Information Into Functions Pass Multiple Arguments to a Function

J A
seal-mask
.a{fill-rule:evenodd;}techdegree
J A
Full Stack JavaScript Techdegree Student 4,646 Points

Is there a way to enforce types for parameters?

In other languages like Java, Python and PHP you can (and in some cases must) declare the type of parameters for a function. Does JavaScript have a notion of type hinting parameters? For example, in this video, there is nothing to prevent someone from calling the getArea() function with a String or Object argument instead of a Number.

Related to that, if there is no type hinting, is there a way to overload a function so that the same function name can be used with different numbers or types of arguments?

1 Answer

Steven Parker
Steven Parker
231,248 Points

JavaScript is a loosely typed language, meaning you don't have to specify what type of information will be stored in a variable in advance. Because of this, it has no equivalent to overloading as found in strongly typed languages.

There is a derivative language called TypeScript that is strongly typed, and it does implement overloading.