1

I am new to JavaScript and programming in general. I am making a small script which basically gives the user a math test and grades it. My question is, how do I make a variable equal the parameter value from a function. For example:

function Function(parameter){
    var var1 = parameter;
}

How do I make var1 equal the parameter passed to the function? Thanks!

Edit:

I have one more question:

Can I make a variable name equal to the parameter passed to the function? For example someone passed the parameter five to the function, could I create a variable with the name of the parameter, five?

vkumar
  • 863
  • 4
  • 9
  • 14
  • 2
    You already did it. :) Just add a `var` before `var1` – Cyval Feb 02 '16 at 00:48
  • Exactly like that. You might want to add a `var` declaration. – Bergi Feb 02 '16 at 00:48
  • 1
    Exactly like that. (Although `parameter` is already a variable so you could remove the line inside the function entirely.) – Quentin Feb 02 '16 at 00:49
  • 1
    And if we understood your question wrong, and you want to dynamically name a variable, then the answer is: You don't do that. Use an array or object instead, properties can be dynamically created and accessed. – Bergi Feb 02 '16 at 00:49
  • Ok Bergi, just updated question before I read that. – vkumar Feb 02 '16 at 00:53
  • Re edit: Don't do that. Variable variables are evil. But Duplicate question is over here: http://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript – Quentin Feb 02 '16 at 00:53

1 Answers1

2

I would highly recommend naming your function something other than Function, since that is actually the type name for functions.

To answer your question, you will want to use the var keyword or else the variable will be declared in the global scope:

function myFunction(parameter)
{
    var var1 = parameter;
}

For the second question, you can, but it is not generally advisable. You could create it on either an existing object (such as window) or on a new object. Below I create a property on a new object with the parameter key passed in, setting the value to true:

function myFunction(name)
{
    var myObj = {};
    myObj[name] = true;
    return myObj;
}

myFunction('foo');
// ==> { foo: true }
Travis Schettler
  • 844
  • 1
  • 6
  • 10
  • 2
    While those are good pieces of advice, they don't actually answer the question (which doesn't seem to be answerable in the first place), so should be comments. – Quentin Feb 02 '16 at 00:50
  • 2
    @Quentin yes, I did answer the question. – Travis Schettler Feb 02 '16 at 00:50
  • Where? You recommended using a different name for the function (good advice, but not needed) and you recommended using a local variable (generally good advice, but not needed). Where's the bit where you actually answer the question "How to make a JavaScript variable equal to a function parameter value"? – Quentin Feb 02 '16 at 00:52
  • @Quentin This is definitely an attempt to answer. For reviewers: this should not have been flagged and should not be recommended for deletion in the LQPRQ. See: [You're doing it wrong: A plea for sanity in the Low Quality Posts queue](http://meta.stackoverflow.com/q/287563/4639281) and [Your answer is in another castle: when is an answer not an answer?](http://meta.stackexchange.com/q/225370/288751) - [From Review](http://stackoverflow.com/review/low-quality-posts/11109376) –  Feb 02 '16 at 02:46
  • @TinyGiant — You comment comes after significant editing and extending of the answer. – Quentin Feb 02 '16 at 08:46
  • @quentin, the first revision was still an attempted answer, my comment stands true for that revision as well. While it may not have been a good answer, it should not have been flagged and should not be recommended for deletion in the LQPRQ. That comment was mostly for the benefit of overzealous reviewers who don't actually review the answers, instead choosing to base their review on the comments alone. –  Feb 02 '16 at 15:23