Javascript Variables

Javascript has the following data types

  • Number : This can be either integer or real.
  • Boolean : True or False values.
  • String : A group of charecters between quotes.
  • Null : This is keword to define a Null value.

Variables in Javascript need not be defined as any data type when they are decleared.

JavaScript Variables

Variables are naming containers used to storing data value and then refer to the data simply by naming the container. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the "var" keyword as follows

<script type="text/javascript">
var isValid = false;
var customerName = "Charlie";
var customerId = 212312;
</script>

Since Javascript is loosely typed, conversion can be done from one type to another type automatically. For Example : "isValid" having a boolean value may be re-assigned a value which is of type string. For Example: isValid = "This is example";

<script type="text/javascript">
var isValid = false;
var customerName;
var customerId;
customerName = "Tom";
customerId = 007;
</script>
<script type="text/javascript">
var customerName, customerAddress;
</script>

JavaScript Variable Declaration Rules

  • Can not use JavaScript reserved keyword as variable name. For example, break or boolean variable names are not valid.
  • JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 000Var is an invalid variable name but _000Var is a valid one.
  • JavaScript variable names are case sensitive. For example, Customer and customer are two different variables.

JavaScript Variable Scope

The scope of a variable is the region of source code in which it is defined. In Javascript a global variable has global scope; it is defined outside function in JavaScript code. On the other hand, variables declared within a function are defined only within the body of the function. They are local variables and have local scope.

Within the body of a function, a local variable takes precedence over a global variable if decleaded with same name. If variables are declared as local or function parameter with the same name as a global variable, effectively hide the global variables:

<script type="text/javascript">
var gscope = "global";   // This is a global variable
function checkVariableScope() {
    var gscope = "local";  // This is a local variable with the same name
    return gscope;  // This will return the local value, not the global one
}
checkVariableScope();   // "local"
</script>

JavaScript Reserved Words

Javascript has following reserved words. They cannot be used as variables name, functions name, methods name, loop labels, or any object names.

abstract
boolean
break
byte
case
catch
char
class
const
continue
debugger
default
delete
do
double
else
enum
export
extends
false
final
finally
float
for
function
goto
if
implements
import
in
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
super
switch
synchronized
this
throw
throws
transient
true
try
typeof
var
void
volatile
while
with