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 Foundations Variables Hoisting

function parameters

Im am not really sure what the function parameters do in the scenario, and my understanding on function parameters seems to be slightly out. Can somebody help explain this? Thanks :)

3 Answers

Stone Preston
Stone Preston
42,016 Points

function parameters are a way to pass data to a function to be used inside of it.

Here is a basic function that accepts 2 parameters

function addTwo(parameter1, parameter2) {
  return parameter1 + parameter2;
}

see how the variables passed in as parameters are used inside the function body?

I could then call the function like so:

var x = addTwo(1,4);

which would assign 5 to x since the function takes whats passed in and adds them together according to the function definition

Its worth noting that when you are defining a function what goes inside the ( ) are called parameters, however when you call a function what goes inside the ( ) are called arguments. so in the above examples parameter1 and parameter2 are parameters since they are in a function definition, however 1 and 4 are arguments since its a function call.

Thanks for reply! Ok I think I get it more now..... so the parameters act as "place holders" almost for future data when being used as a variable? Without using it later as a variable seems pointless though.... is this the case? excuse my ignorance im VERY new to this language :)

Stone Preston
Stone Preston
42,016 Points

yes thats exactly right. placeholders is a good way to think of it. the addTwo function is pretty pointless yes (especially since you can just add two numbers together using the + sign) but it was just a basic example.

Ok this is cool and helps alot, I am just still struggling to understand the usage of the parameter on the lesson I was just watching ( Hoisting on javascript foundations ) There is a parameter used in this lesson which is confusing me. Are you able to help on this?

Stone Preston
Stone Preston
42,016 Points

is it the doit parameter thats confusing you?

I got it now, as the parameter (doit) is made true it runs the part of code and when false does not run the else statement! Makes sense, thanks for the help!

Stone Preston
Stone Preston
42,016 Points

yes that parameter is a boolean (which means it can be true or false)

Programming can be a nightmare at first XD But when you get it its fun! Do you have any advice for new programmers like me who want to use javascript for the purpose of adding interactivity and a sense of motion to websites? ( My initial plan was to use jquery but its not easy without knowing the basics of JS ) As right now I struggling to see how i can apply what I have learnt to a website.

Stone Preston
Stone Preston
42,016 Points

you will get there. this sort of thing is a very basic concept that the things you need to know to create interactivity are built on. Its probably good you didnt dive right into Jquery. learning this first will help.

Awesome dude thanks a million!