[SalesForce] Global Variables JS – Component Controller

So I have a global variable defined in my code and it is throwing an error. The error is

Failed to save undefined: 0Ad1F0000000meq:
org.auraframework.util.json.JsonStreamReader$JsonStreamParseException:
Only functions are allowed in javascript controllers [3, 17]: '10':
Source

My code is in JavaScript. It is a controller for my component. I want to declare a global variable but am I declaring it correctly?

Simplified code:

({
  x: 10,   /* Global variable */
  y: 15    /* Global variable */

  function1 : function(..){
  },
  ....
})

Best Answer

You can declare those variables in helper and access them like that:

({
    x: 10,
    y: 15,
    someHelperFunction: function() {
        console.log(this.x);
    }
})

or from the controller:

({
    someControllerFunction: function(component, event, helper) {
        console.log(helper.x);
    }
})
Related Topic