0

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

1
  • I don't quite get its purpose It invokes getName method with two parameters just initialized before this line. Also, I'm not to clear on the use of the parameters of the function Can you share precisely what doubts you have? Commented Jul 11, 2016 at 6:36

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Well said and I agree, except you pass arguments, not parameters.
0

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).

2 Comments

So basically, after getting the input (1st & last name) from the prompts, the getName() holds those name, calls and passes those two parameters to the function. Then the function does its thing??
@J. doyevski getName(first, last); is invoking the function. It says, store the value of first in var1, store the value of last in var2, and execute the function body.
0

Function declarations create a variable, with the same name as the function, in the current scope and are hoisted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.