Home » Online Computer Education » Learn How to Global Variable Create in Java Script, Lesson 11

Learn How to Global Variable Create in Java Script, Lesson 11

 

Global Variables in Java Script.
A global variable is any variable defined outside of a function. Such a variable is global because any script statement located in the Web page or script file can refer to it. You can create a global variable in several ways, including:

Creating the variable by referencing it inside a function without first declaring it using the var keyword.

Creating the variable anywhere else in a JavaScript with or without the var keyword.

The following example demonstrates the differences between local and global variable scope. Three variables are created with a global scope. The first is glVarMsg1 in the head section of the page. The second and third variables are glVarMsg2 and glVarMsg3 in the body section of the page. The glVarMsg2 variable is global in scope even though it is located within a function because it was not created using the var keyword. The lcVarMsg1 variable is local in scope because it is in a function and was created with the var keyword.

<HTML>
<HEAD>
<TITLE>Script 2.3 – A demonstration of variable
scope</TITLE>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
glVarMsg1 = “Global variables created in the HEAD
section can be ” +
“referenced anywhere in this page.”;
function CreateVariables()
{
var lcVarMsg1 = “Local variables created inside a
function cannot ” +
“be referenced anywhere else.”;
glVarMsg2 = “Global variables created inside functions
in the HEAD ” +
“section can be referenced anywhere in this page.”;
}
// End hiding JavaScript statements –>
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
CreateVariables();
glVarMsg3 = “Global variables created in the BODY
section can be ” +
“referenced anywhere in this page.”;
document.write(glVarMsg1 + “<BR>”);
document.write(glVarMsg2 + “<BR>”);
document.write(glVarMsg3 + “<BR>”);
document.write(lcVarMsg1 + “<BR>”);
// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>

Both x++ and ++x operators increment the value of x by 1. The difference between them is when the update occurs. I can best demonstrate this difference with an example. Suppose that I have a script with two variables, totalCount and noUnitsSold. Suppose that noUnitsSold is equal to 10 and that I added the following line of code somewhere further down in my script:

totalCount = ++noUnitsSold;
What happens here is that first noUnitsSold would be incremented by 1 to a value of 11. Then totalCount would be set to 11. Now suppose that I rewrote the assignment statement to be the following:

totalCount = noUnitsSold++;
What happens here is totally different than in the preceding example. This time, totalCount is first set to the value of noUnitsSold, which is 10. Then the value of noUnitsSold is incremented by 1 to 11.

The x and x operators work the same way, only they decrease the value of the variables by 1.

Assigning Values
As I have already alluded to, you assign values to variables using an assignment operator. For example, the following statement assigns a value of 44 to a variable named totalCount:

totalCount = 44
In this example, the assignment operator is the old-fashioned equals sign. JavaScript and JScript provide many ways to assign values to variables
Only global variables or those local in scope to the document. write() statements will appear when the page is displayed.

javascript global variable

 

 

 

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *