4

I apoligize for my newbness but I'm not clearly understanding how to access variables within a function outside of the function. I understand that you need to pass them some how but I'm not 100% sure on the whys and hows.

Taking the below code for example, I want to use the var degree throughout the code outside of the function. How do I do it?

function DegreeToMil() 
{

//Degree's to Mils: 1 Degree = 17.777778 Mils

var degree = 10;
var mils = degree * 17.777778;

return mils;

 }
11
  • A bad answer would be to define it without the var which makes it global... a better answer would be to return the vars that you want to use at the end of the function Commented Aug 5, 2013 at 0:41
  • Yea I understand I can make it global but let's say this in this scenario i don't want to. Commented Aug 5, 2013 at 0:42
  • Then you can pass variables to the function, read them inside and do what you want to do, and at the end - return them - that would be a better approach. Commented Aug 5, 2013 at 0:43
  • So why can't I access the var degree by just typing DegreeToMil.degree Commented Aug 5, 2013 at 0:45
  • 1
    @mwilson I updated my answer below, so you can check it out. Note, unless you called DegreesToMils() with a defined parameter prior to attempting to access DegreesToMils.degrees without including the definition line I included above the function, it will resolve as undefined. You may or may not care about it. Also, I did try making it so you wouldn't need the line outside of the function, but it would have still required a function call first. If someone else knows another way I hope they share it. Commented Aug 5, 2013 at 1:14

2 Answers 2

4

It is actually fairly simple, just define it outside of the function.

Edit: Updated with an example, and comments explaining what was done and how it works.

    DegreesToMils.degrees = 10; /* This is a static variable declaration to make sure it isn't undefined
                             * See note 1 below */

function DegreesToMils(degrees) {
    if (degrees !== undefined) {
        DegreesToMils.degrees = degrees; /* If the parameter is defined, 
                                          * it will update the static variable */
    }

    var milsPerDegree = 17.777778; /* This is a variable created and accessible within the function */

    return DegreesToMils.degrees * milsPerDegree; /* The function will return 177.77778 */
}

console.log(DegreesToMils.degrees); /* Prints 10, Note 1: This would be undefined if
                                     * not declared before the first call to DegreesToMils() with a 
                                     * defined parameter
                                     */
console.log(DegreesToMils(10)); /* Prints 177.77778 */
console.log(DegreesToMils(9)); /* Prints 160.00000200000002, Sets DegreesToMils.degrees to 9 */
console.log(DegreesToMils.degrees); /* Prints 9 */
Sign up to request clarification or add additional context in comments.

2 Comments

I just wrote that this would be a bad answer :)
We posted at the same time ;) I defined mils outside of the function using var, and assigned to it inside the function. Depending on the environment you are in, for example node.js, defining it as a var outside of the function will only make it global to the containing module, and will not put it in "global space".
1
function DegreeToMil() 
{

    //Degree's to Mils: 1 Degree = 17.777778 Mils

    var degree = 10;
    var mils = degree * 17.777778;
    var result = [degree, mils]; // it's an array
    return result;
}

// use it like this

var myResult = DegreeToMil();
console.log(myResult[0]); // degree
console.log(myResult[1]); // mils

3 Comments

EDIT changed brackets to parens
So the only option for me to use degree outside of the function to return it?
what about function DegreeToMil (degree)?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.