Introducing JavaScript Variables

From Techotopia
(Redirected from JavaScript Variables)
Jump to: navigation, search
PreviousTable of ContentsNext
Embedding JavaScript into Web pagesJavaScript Variable Types


Purchase and download the full PDF version of this JavaScript eBook for only $8.99


Variables are a key element of just about every programming language ever invented, and just like other programming languages, an understanding of the basics of JavaScript variables is key to learning this scripting language.

Variables are nothing more than a way to store information in memory. A variable can be a number, a single character, a string of characters making up a word or sentence, or a value indicating whether something is true or false. In JavaScript you declare a variable by giving it a name and assigning a value to it. You can then access this data at any time and, more importantly, change the content of the variable whenever necessary (hence the name variable).

Variables are assigned and manipulated using operators which will be covered in JavaScript Operators. At this stage it is important to understand one operator in particular - the assignment operator.

Variable Declaration and the Assignment Operator

Variables in JavaScript are declared by specifying the name of the variable storage you wish to create prefixed with the token var. For example, to create a variable named myVariable:

var myVariable;

The above line creates a new variable which can be accessed using the name myVariable but does not assign any initial value.

Note that whilst it is also possible to declare a variable without the var token this is strongly discouraged. Failing to specify the 'var' before the variable can cause confusion when variables with the same name are present in local and global scopes. Good practice dictates that all variable declarations be prefixed with the var token.

The '=' assignment operator can subsequently be used to assign a new value to our variable:

myVariable = 10;

In this instance the variable which had already been declared is assigned an integer value of 10.

In addition, the variable can be initialized at the same time that it is declared. For example:

var myVariable = 10;

The above example achieves the same result as the following script sequence:

var myVariable;
myVariable = 10;

It is also possible to declare and assign multiple variables on a single declaration line. For example:

var i, j, k;
var myValue = 9, myMessage = "Welcome to JavaScript"; 

In the first line above the variables i, j and k are all defined but are not assigned initial values. The second line declared both myValue and myMessage, initializing the variables with values. In the case of myMessage the variable is initialized with a string.

JavaScript Variable Naming Conventions

JavaScript variable names are made up of letter, numbers and underscores. They cannot contain spaces and must not begin with a number. For example sales_percentage would be a valid variable name, where as sales percentage would be invalid because it contains a space character. Similarly two_layers is valid, but 2_layers is not due to the fact that it begins with a number. JavaScript variable names can be of any length, although common sense should be used to ensure excessively long name are not used.


Purchase and download the full PDF version of this JavaScript eBook for only $8.99



PreviousTable of ContentsNext
Embedding JavaScript into Web pagesJavaScript Variable Types