Frequently you need to pass specific values to functions, and the functions will use those values when executing their code. You pass values to a function in comma-delimited form. A function definition needs a list of variable names in ()
that match the number being passed in. For example, the following function accepts two arguments, name
and city
, and uses them to build the output string:
function greeting(name, city){
console.log("Hello " + name);
console.log(". How is the weather in " + city);
}
To call the greeting()
function, you need to pass in a name
value and a city
value. The value can be a direct value or a previously defined variable. To illustrate this, the following code executes the greeting()
function with a name
variable and a direct string for city
:
var name = "Brad";
greeting(name, "Florence");