Javascript Reference Guide

 
 
This guide contains useful Javascript tips.







Local and global variables.



Information



Variables with the keyword var are local variables.
Variables without keyword var are global variables.

Examples
<script type="text/javascript" language="JavaScript">

a = 1;

jsxDisplay();

function jsxDisplay() {
   a = 2;
   // Value a inside the function =2
   alert("Value a inside the function =" + a);
}

// Value of a outside the function = 2
alert("Value of a outside the function =" + a);
</script>

Show demo




<script type="text/javascript" language="JavaScript">

var a = 1;

jsxDisplay();

function jsxDisplay() {
   a = 2;
   // Value a inside the function = 2
   alert("Value a inside the function =" + a);
}

// Value of a outside the function = 2
alert("Value of a outside the function =" + a);
</script>
Show demo




<script type="text/javascript" language="JavaScript">

a = 1;

jsxDisplay();

function jsxDisplay() {
   var a = 2;
   b = 10;
   // Inside the function a=2, b=10
   alert("Inside the function a="+a+", b="+b);
}

alert("Outside the function a="+a); // Outside the function a=1
alert("Outside the function b="+b); // Outside the function b=10
</script>
Show demo




<script type="text/javascript" language="JavaScript">

var a = 1;

jsxDisplay();

function jsxDisplay() {
   var a = 2;
   var b = 10;
   // Inside the function a=2, b=10
   alert("Inside the function a="+a+", b="+b);
}

alert("Outside the function a="+a); // Outside the function a=1
alert("Outside the function b="+b); // Error b is undefined
</script>
Show demo