Reputation: 3
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);
}
Jarad
Upvotes: 0
Views: 63
Reputation: 130
getName is a function in your JavaScript code which have two parameter var1 & var2. This function simply concatinate these two parameters with your text "The name is " and show in an alert message.
And the line getName(first,last); is calling your function getName() & passing two arguments first & last. You can pass any two arguments to this function and it will show it in an alert.
e.g: instead of using first & last, you can use the below code and see the result.
var x = 'Mr';
var y = 'Ayaz';
getName(x,y);
Upvotes: 1
Reputation: 548
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).
Upvotes: 0
Reputation: 944441
Function declarations create a variable, with the same name as the function, in the current scope and are hoisted.
Upvotes: 0