Mastering Variables


© Muhammad Ali Shah



Variables Defined:

Variables in a programming language are similar to those in Mathematics in one manner: a variable is some named entity that has no fixed value. The digit 9, for example, is not a variable because it has a fixed value. On the other hand, we can say x=6 is a variable because it can be re-assigned a new value whenever required. A variable is important only when it has been assigned a value.


Variable Naming Rules:

As computers work with many types of data (alphanumeric, digits, images, etc.), the variables in a programming language can represent anything from an alphanumeric string to an image. In JavaScript, the name of a variable should start with an alphabet followed by any combination of alphabets, digits and underscores. Thus totalCost and day5 are legal while 5day and total@Cost are illegal variable names.

Another important thing to note is that JavaScript is case sensitive. Thus, totalCost refers to a different variable then both TotalCost and totalcost.

With regard to variables two things are very important: Scope and Data Type.



Scope of a Variable:

Scope is defined as the visibility of a variable. If you don't know how to write JavaScript functions then understanding scope may be difficult for you. Leave it if you feel your head spinning.

Programming languages usually allow two broad types of variables: local and global. A local variable is one that can be used in a single function. If you try to change / read the value of a local variable from outside that function, an error will be reported. A global variable, as the name suggests, can be read / changed from any function on a single page.

You don't need to do anything special to incorporate scope of a variable - its automatically handled for you. A variable that is defined in a function (we will see how to define a variable shortly) is local. A variable defined outside any function is global. These logical concepts are simply for making you a better programmer.



Using Variables:

A variable is defined as var variableName;. The part var and the semicolon are part of the syntax and will always be used like this. variableName is the name of the variable that you want to define.

Values are assigned as variableName = value. For example to mean "let i be equal to 10", we write i=10;. Some examples are given below:

Go To Page: 1 2 3


The copyright of the article Mastering Variables in JavaScript is owned by . Permission to republish Mastering Variables in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo