Define global variable in a JavaScript function

Define global variable in a JavaScript function

Is it possible to define a global variable in a JavaScript function?
I want use the trailimage variable (declared in the makeObj function) in other functions.







Solutions/Answers:

Solution 1:

Yes, as the others have said, you can use var at global scope (outside of all functions) to declare a global variable:

<script>
var yourGlobalVariable;
function foo() {
    // ...
}
</script>

Alternately, you can assign to a property on window:

<script>
function foo() {
    window.yourGlobalVariable = ...;
}
</script>

…because in browsers, all global variables global variables declared with var are properties of the window object. (In the latest specification, ECMAScript 2015, the new let, const, and class statements at global scope create globals that aren’t properties of the global object; a new concept in ES2015.)

(There’s also the horror of implicit globals, but don’t do it on purpose and do your best to avoid doing it by accident, perhaps by using ES5’s "use strict".)

All that said: I’d avoid global variables if you possibly can (and you almost certainly can). As I mentioned, they end up being properties of window, and window is already plenty crowded enough what with all elements with an id (and many with just a name) being dumped in it (and regardless that upcoming specification, IE dumps just about anything with a name on there).

Instead, wrap your code in a scoping function and use variables local to that scoping function, and make your other functions closures within it:

<script>
(function() { // Begin scoping function
    var yourGlobalVariable; // Global to your code, invisible outside the scoping function
    function foo() {
        // ...
    }
})();         // End scoping function
</script>

Solution 2:

UPDATE1: If you read the comments there’s a nice discussion around this particular naming convention.

UPDATE2: It seems that since my answer has been posted the naming convention has gotten more formal. People who teach, write books etc. speak about var declaration, and function declaration.

UPDATE3: Here is the additional wikipedia post that supports my point: http://en.wikipedia.org/wiki/Declaration_(computer_programming)#Declarations_and_Definitions

…and to answer the main question. DECLARE variable before your function. This will work and it will comply to the good practice of declaring your variables at the top of the scope 🙂

Solution 3:

Just declare

var trialImage;

outside. Then

function makeObj(address) {
    trialImage = [address, 50, 50];
..
..
}

Hope this helps.

Solution 4:

Just declare it outside the functions, and assign values inside the functions. Something like:

<script type="text/javascript">
    var offsetfrommouse = [10, -20];
    var displayduration = 0;
    var obj_selected = 0;
    var trailimage = null ;  // GLOBAL VARIABLE
    function makeObj(address) {
        trailimage = [address, 50, 50];  //ASSIGN VALUE

Or simply removing “var” from your variable name inside function also makes it global, but it is better to declare it outside once for cleaner code. This will also work:

var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;

function makeObj(address) {
    trailimage = [address, 50, 50];  //GLOBAL VARIABLE , ASSIGN VALUE

I hope this example explains more: http://jsfiddle.net/qCrGE/

var globalOne = 3;
testOne();

function testOne()
{
    globalOne += 2;
    alert("globalOne is : " + globalOne );
    globalOne += 1;
}

alert("outside globalOne is : " + globalOne);

testTwo();

function testTwo()
{
    globalTwo = 20;
    alert("globalTwo is " + globalTwo);
    globalTwo += 5;
}

alert("outside globalTwo is :" + globalTwo);

Solution 5:

No, you can’t. Just declare the variable outside the function. You don’t have to declare it at the same time as you assign the value:

var trailimage;

function makeObj(address) {
  trailimage = [address, 50, 50];

Solution 6:

It is very simple define the trailimage variable outside the function and set its value in makeObj function. Now you can access its value from anywhere.

var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
var trailimage;
function makeObj(address) {
      trailimage = [address, 50, 50];
      ....
}