JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.00

Declaring Variables - "var" Statements

This section provides descriptions on variables and a tutorial example on how to declare variables with 'var' statements.

What are variables? Variables are symbolic names that refer to values stored in the memory.

Variable names must be sequences of alphanumeric characters and underscore (_). But the first character can not be a numeric character. (_5star) is a valid variable name. But (5star) is not an invalid variable name.

Variable can be used without declaration. But it is strongly recommended that all variables are declared with "var" statements in the following syntax formats:

var var_name_1;
var var_name_1 = value_1;
var var_name_1, var_name_2, var_name_3, ...; 
var var_name_1 = value_1, var_name_1 = value_1, ...;

If a variable is declared without initial value, it contains the "undefined" value.

Here is a JavaScript tutorial example that shows you how to declare variables with "var" statements:

<html>
<!-- Variables.html
   Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head><title>Variables</title></head>
<body>
<pre>
<script type="text/javascript">
   var site_url = "herongyang.com";
   var site_rank = 3;
   var site_content;

   document.write(site_url);
   document.write('\n');
   document.write(site_rank);
   document.write('\n');
   document.write(site_content);
   document.write('\n');
</script>
</pre>
</body>
</html>

The output of this sample JavaScript is:

herongyang.com
3
undefined

Sections in This Chapter

Primitive Data Types - Numbers, Strings, and Booleans

Numeric Value Literals

String Literials

Declaring Variables - "var" Statements

Operators and Expressions

Operators and Expressions - Examples

Dr. Herong Yang, updated in 2008
Declaring Variables - "var" Statements