Javascript

JavaScript Variables

JavaScript Variables

A variable is a “container” for data or information you want to store.

A variable is an area inside the memory set aside to store certain data, or you can call it a “container” that stores your data.

In a matter of speaking you can put data into the container, retrieve data from it or change the contents of this “container”.You refer to the variable by its name (variable identifier). Remember that in Java Script variable names are case sensitive thus “age” is not the same as “AGE” or “Age”. All variable names must begin with letter or the underscore (_) character.Java Script is an untyped language.This means that Java Script variables can hold data of any valid type.It takes its type from the data type of what it is holding.You cannot declare a type for variables in Java Script.There is no facility for saying this variable must be a number,or this one must be a string.

Declaring the variables

Variables name can not contain spaces, or begin with any number, and all punctuation symbols except for the underscore (_) are restricted. Your variable names should be descriptive of the value they represent if for no other reason than script readability. Be careful to not use reserved Java Script keywords for your variable names. A common convention to help avoid using reserved words is using two (or more) word combinations. The two words can either be separated by an underscore (my_variable) or by capitalizing the second word (myVariable). While I prefer the latter, the convention you choose is up to you.

Variable Scope

Variables scope has to do with where a variables can legally be used, and is determined by where it was originally declared or initialized. A variables declared or initialized outside a function body has a global scope, making it accessible to all others statement within the same document. A variables declared or initialized within a function body has a local scope, making it accessible only to statements within the same function body.

Variable Scope – Special Case

If variables are initialized inside a function body but the var keyword is left off, the variables have a global scope and this is only accessible after the function containing it is invoked. It is safer to always use the var keyword, begining or pro.
You declare variables in JavaScript with the var keyword. You can declare multiple variables at once. You can also declare a variable and assign it a value at the same time. Until you assign a value to a variable it is undefined.
If you try to declare a variable that already maych or existsin same program, Java Script will treat it as a simple assignment statement and assign any new value in the declaration statement to the variable.If the duplicate declaration has no assignment, then nothing happens. If you try to assign a value to a non-existent or non match variable, Java Script will create the variable for you.
Here is some code creating and assigning values to a couple of variables:

<script type="text/javascript">
//Commented lines starting with the double
//forward slash will be ignored by JavaScript

//First we will declare a few variables
//and assign values to them

myText = "Good day!";
myNum = 5;
//Note that myText is a string and myNum is numeric.

//Next we will display these to the user.
document.write(myText);

//To concatenate strings in JavaScript, use the '+' operator
document.write("My favourite number is "+ myNum);
</script>

Variable Naming Conventions

JavaScript variables must start with a letter or underscore “_”.JavaScipt is case sensitive.

Case sensitivity (small and big letter in the code is not same)is the thing that causes many problems and take hours of finding mistakes. Have a look at the following code:

<script type="text/javascript">

var myVar = "WebCheatSheet";
var myvar = "Paktutorial JavaScript Tutorial";

//outputs "WebCheatSheet - Paktutorial JavaScript Tutorial"
document.write(myVar + " - " + myvar);
</script>

Variable Scope and Lifetime

All variable has scope. The scope is the region of the program for which the variable is declared. Variables are either Global,or Local.Local variables are available only within the section of code in which they were defined.Changes to Local variables are not reflected ‘outside’ their area of definition.When you exit this area, the variable is autometic destroyed. Global variables are available to other Java Script code. Generally, variables declared as “var” are Global variables. Variables declared inside a function as “var” are local to the function. Variables defined inside a function without the “var” are Global variables. The lifetime of Global variables starts when they are declared, and ends when the page is closed.

<script type="text/javascript">
var altitude = 5;            //GLOBAL

function square( ) {
base = 17;             //GLOBAL
sqr = 0.5*(base + altitude);
return sqr;
}

function perimeter() {
var side = 7.5;             //LOCAL
prm = 2*side + base;
return prm;

}
</script>

Local JavaScript Variables

A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function. (the variable has local scope).

You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

Local variables are destroyed when you exit the function.

You will learn more about functions in a later chapter of this tutorial.


Global JavaScript Variables

Variables declared outside a function becomes GLOBAL, and all scripts and functions on the web page can access it.

Global variables are destroyed when you close the page.

If you declare a variable, without using “var”, the variable always becomes GLOBAL.

DOWNLOAD SOLUTION HERE