11.06.07

JavaScript Variables

Posted in JavaScript at 12:30 pm by admin

In order to truly understand JavaScript you need to understand JavaScript variables.  A variable is a word or a string that is responsible for containing information that you want to store in the JavaScript code.  While the value of a variable can change during the script, you can also refer to the variable by the exact same name throughout the script once it has been initially defined.  There are only two rules for defining variables.

The first rule is that variable names are case sensitive.  This means that if you capitalise any letters in the variable name, it also means that you need to capitalise those letters elsewhere in the code for the computer to understand what you are talking about.

The second rule is that a variable defined in JavaScript must begin with the letter or the underscore character.  If you do not define your variables with either a letter or the underscore character at the start of the variable than the browser will not be able to recognize it as a variable.  As long as you stick to these two simple yet extremely important rules, you should have no problem defining various variables within your JavaScript code.

In order to illustrate these examples more clearly, let us take a look at a few simple variables as defined within the JavaScript code.

var strname = some value

This is an example of a variable that has been created through the use of the var statement.  While this is certainly a correct example of a way to create a variable in JavaScript, at the same time you will find that usually the var statement is not necessary.  In the above example, the same thing can be accomplished with the below piece of code.

Strname = some value

This might seem like splitting hairs to you, but when you are confronted with a complicated set of JavaScript code, you are going to be thankful if the person that programmed the code understood the concept of reducing redundancies in their variable statements.

Depending on where you declare your variables, they are either going to be local variables or global variables.  Local variables are defined within a specific JavaScript function and therefore when that function is over, the variables no longer apply.  Global variables, on the other hand, are defined outside of a function, meaning that all of the JavaScript functions on a single page can use them.  There is currently no easy method to define variables beyond a single page of functions, which is why Global variables are given that name.

Scriptycan is a great software repository featuring both free and commercial JavaScript scripts and applications for developers and programmers.

Leave a Comment

You must be logged in to post a comment.