J. Doyevski J. Doyevski - 1 year ago 61
Javascript Question

JavaScript functions with arguments

I'm new to JavaScript and making good progress. When I got to this part I was a little confused. Mostly the part that says "getName(first, last)". I don't quite get its purpose and what it does in detail. It wasn't declared with a "var" nor is it within the function, or "=" to anything. Also, I'm not to clear on the use of the parameters of the function. I'd really appreciate any help. I'm new to SE too and I'm loving it. Thanks in advance

var first = prompt("May I have the First Name");
var last = prompt("May I have the Last Name");
getName(first, last);
function getName(var1, var2) {
var x = var1 + " " + var2;
alert("The name is " + x);


Answer Source

getName(first, last); is a function call. You're providing the function with two pieces of data (first name and last name) and instructing it to execute. The data you're providing it are called arguments, the identifiers in the function declaration are called parameters. The names do not need to match. Inside the function, the names it uses are the parameter names.

When you call getName(first, last); Control jumps to that function and the body (the part inside {} is executed).