Home page
or browse popular tags

Sign up for the free email newsletter for new tips, tutorials and more. Enter your email address below, and then click the button.

Privacy Policy

RSS Twitter

Javascript: Variables


A variable's purpose is to store information so that it can be used later. A variable is a name, or identifier, that represents some data that you set. The name wraps up the data so you can move it around a lot easier, but the name is not the data! A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value.

JavaScript is an untyped language. This means that JavaScript 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 JavaScript. There is no facility for saying this variable must be a string, or this one must be a number.

JavaScript also converts types as needed, automatically and behind the scenes.

Declaring the variables

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 exists, JavaScript 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 variable, JavaScript 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 = "Have a nice day!";
myNum = 5;
//Note that myText is a string and myNum is numeric.

//Next we will display these to the user.

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

Variable Naming Conventions

  • JavaScript variables must start with a letter or underscore "_".
  • JavaScipt is case sensitive. 

Case sensitivity 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 = "JavaScript Tutorial";

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


Variable Scope and Lifetime

All variables have scope. The scope is the region of the program for which the variable is declared. Variables may be 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 destroyed. Global variables are available to other JavaScript 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;



Add To: Add to dzone dzone | Digg this digg | Add to del.icio.us del.icio.us | Stumble it stumbleupon

  • Comments

Copyright © 2005-2023             www.WebCheatSheet.com All Rights Reserved.