JavaScript Variables, JavaScript Tutorial, Global Guide Line Technology.
Forum | Blog |


Basic JavaScript Guide.
 JScript Introduction.            
 JScript Why?                       
 JScript Recognitions.          
 JScript onMouseover.         
 JScript with Buttons.           
 JScript Alerts.                     
 JScript Variables.
 JScript Operators.               
 JScript Functions.               
 JScript Prompts.                 
 JScript Conditions.             
 JScript Validations.            
 JScript Confirmation Boxes.
 JScript Browser Detection.  
 JScript Redirection.            
 JScript on Links.                 
 JScript Switch.                    
 JScript Loops.                     
 JScript Events.                    
 JScript Exception Handling.
 JScript Reserve Characters. 
 JScript with Objects.            
 JScript Math Object.           
 JScript using Dates.             
 JScript using String.            
 JScript using Arrays.            
 JScript Operations.              
 JScript Handling Cookies.   
 JScript in Mapping.             
 JScript Time Out.                
 JScript Summary.                

JavaScript Tutorial >> JavaScript Variables.

     Back             Next     
Here JavaScript Tutorial will guide us that the JavaScript Variables are used in Microsoft JavaScript to store values in your JavaScript. JavaScript Variable is a way to retrieve and manipulate values using names. When used effectively then can help in understanding what a JavaScript does.

Declaring JavaScript Variables

Although not required to declare a JavaScript Variable, it is considered good practice to declare JavaScript Variables before using them. You do this using the var statement in JavaScript. The first time you must use the var statement when declaring JavaScript Variables that are local to a JavaScript Function. At all other times, using the var statement to declare JavaScript Variables before their use is a recommended practice.

Syntax and data types of JavaScript Variables

var variable1 = "I am using my first JavaScript variable in JavaScript Tutorial!"; 
// The value stored in variable1 is of string data type.
// The sentence in quotes, the value of which is assigned to variable1, is a string literal.

var variable2 = 3;       
// The value stored in variable2 has numeric data type.

var variable3 = true;       
// The value stored in variable3 has Boolean type.

var variable4 = 2.718281828       
// The value stored in variable4 has numeric type.

Naming JavaScript Variables

JavaScript is a case-sensitive language, so naming a variable Variaiable1 is different from naming it VARIABLE1. In addition, JavaScript Variable names, which can be of any length, must follow certain rules below in JavaScript Tutorial.
  • The first character must be a letter (either uppercase or lowercase) or an underscore (_), or a dollar sign ($).
  • Subsequent characters can be letters, numbers, underscores, or dollar signs in JavaScript Variables.
  • The JavaScript Variable name can't be a reserved word of JavaScript, see details of JavaScript Reserved Characters.
Some examples of valid JavaScript Variable names are as under.
  • _Variable
  • Variable9
  • Variable_6
Some invalid JavaScript Variable names are as under.
  • 99Cars // Starts with a number of JavaScript Variable is not allowed
  • Jhon&Smith // Ampersand (&) is not a valid character for use in JavaScript Variable names.
In instances in which you want to declare a JavaScript Variable and initialize it, but without giving it any particular value, you may assign it a special value, as null.  

var variable9 = null;
var variable8 = 3 * variable9;        // At this point, Variable8 becomes 0 as result.

If you declare a JavaScript Variable without assigning any value to it, it exists but is undefined.

var variable1;
var variable2 = 1 * variable1;
// Places the value NaN in variable2 as variable1is undefined.

You can declare a variable implicitly without using var statement in JavaScript by assigning a value to it. You cannot, however, use a variable that has never been declared at all. To do so generates an error at runtime.

variable4 = ""; 
// The variable variable4 is declared implicitly.

var variable5 = var1 + var2;  // Generates an error because var1 and var2 don't exist.

Limitations of JavaScript Variables

Now might be a darn good time to discuss JavaScript Variable limitations to this process.
To begin with, the information is not saved after each surfing like it is with a JavaScript Cookie. In order for this to work, you must ensure the user moves in a linear fashion. Returning and backing up can harm the information being carried.

Next, the way I have this set up, you can only transfer two variables from page to page. You'll see why in a moment.

Also, the method I have written here isn't very friendly to spaces. If the user puts a space in either of their two text boxes, that space will be replaced by a plus sign. If you're going to accept spaces, you'll either have to live with that or write some extra code to eliminate it.

In addition, this is done with JavaScript so there are browsers that will not be able to play with it. I have written my code in JavaScript 1.0 so the most browsers can understand what is to happen. I saw a suggestion on doing this by setting the answers to an array. It's clever coding but it's in JavaScript 1.2 which means bunches of browsers will throw errors. The JavaScript Array method allows for endless JavaScript Variables being passed but limited browsers. Mine allows for the most number of browsers to be able to play with the code but only two JavaScript Variables. You decide.

     Back             Next     

JavaScript Variables Examples

JavaScript Array Object Examples

[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright 2005 -  2023 All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 335 visitors are online now