top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the Correct way to declare Global Variable in Java Script ?

0 votes
477 views
What is the Correct way to declare Global Variable in Java Script ?
posted Jul 14, 2014 by Amarvansh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The proper way is to use window object.

And use the syntax like this:

var window.iAmGlobal = "some val"; //Global variable declaration with window.

//Any place in other part of code

function doSomething()
{
    alert(window.iAmGlobal); //I am accessible here too !!
    //OR
    alert(iAmGlobal); //I am accessible here too !!
}

By defining globals this way, you will make JavaScript more robust and reliable.

Note:

Define global variables with window keyword in form of “window.VAR_NAME”
You can access the variable with “window.VAR_NAME” or directly “VAR_NAME”
Do not use other variables with same name otherwise it may cause unwanted results

answer Aug 5, 2014 by Madhavi Latha
Similar Questions
+1 vote

Please provide some sample code.I need to take copied data's from clipboard .Any one help me to solve this .

0 votes

Also,tell me what are the types of framework are there ?

+3 votes

What is the difference between VB script and java script?

0 votes

I need to execute some function while pasting in web page. Can any tell me to solve this issue?This can be possible in Java Script or Jquery?

...