Watching:

How to Set Function Parameters and Arguments in JavaScript


show more By using parameters and arguments, functions can be designed to accept different information passed to it each time its called. In this video, Simon covers the ins and outs of setting function parameters and arguments, as well as some built-in JavaScript functions like Prompt that expect arguments. show less
please wait ...

Setting parameters and arguments

So I have a simple function here called addTwoNumbers. It just creates two variables, adds them together, and displays the result. And that's fine, but of course this isn't very useful because it's always adding the same two numbers together, always 5 and 10, no matter how many times we call it. Well, I'd like something a bit more flexible than that. I'd like this function to be able to take in some different information to add different numbers together. And I can do that by defining parameters for this function, to say that whenever we call it, we're going to pass information into it.

That's what the parentheses are for after the function name. They allow us to say do we pass one piece of information into this or two or three or a dozen. And all we need to do here is give it the name of what will become two variables. In this case what I could do is remove my variable declaration here, var a and var b, and just say that this function takes in two parameters...

Setting parameters and arguments
Video duration: 6m 7s 4h 47m Beginner

Viewers:

By using parameters and arguments, functions can be designed to accept different information passed to it each time its called. In this video, Simon covers the ins and outs of setting function parameters and arguments, as well as some built-in JavaScript functions like Prompt that expect arguments.

Subjects:
Developer Web
Author:
please wait ...