Your Guide to Professional
Web Site Design and Development

HTML Codes
Web Development
Web Design Tips
JavaScript Codes
216 Web Safe Colors
CSS Tutorial
JavaScript Tutorial
ASCII Character Codes


| Web Site Development | HTML Codes | HTML Tips | Web Design TipsJavascript Snippets | 216 Safe Colors | Symbols | CSS Tutorial | JavaScript Tutorial |

JavaScript Tutorial: JavaScript Variables and Arrays

Variables are simply numbers, letters, or words. They are used to connect to the value that is assigned to the variable. In JavaScript, the only thing that you have to define is a variables name (number or text) and its value.

In JavaScript code, a variable is used as var. Var is followed by the variables name. For instance, if you wanted to set a variable for a number, such as the number 16, the code would look like this:

var number = 16

A variable can also have a value that is a string of text, such as:

var text = "Santa Clause Is Coming To Town"

Note that anytime you use a string, the string must be enclosed inside quotation marks. Also note that variable names can contain numbers, but must always start with a letter. Our two variable names from the samples above are number and text. The first letter in the name is lower case, but if there are two words that are put together, such as numberText the first letter of the second word is capitalized.

A variable needs more to make it do anything. In our example above, var number = 16 doesn't actually do anything. It needs more code to make something happen - to make the variable matter.

Let's make JavaScript print something on our page using a variable.

<script type="text/javascript">
var number = 16;
var text = "Many years ago when";
document.write(text + " I was " + number);

In the sample above, we open the script with the <script> tag. We then set our variable name, which is the word number. We assign a value of 16 to that variable. Then, we write a variable named text, which has a string value of Many years ago when. Note that each variable value ends with that semicolon. That is very important. All elements must be separated with the semicolon, or the script won't work.

Next, we use some JavaScript that you have come to know and love. We assign an object (document) and a method (write), and then we tell it what to write. Text + I Was (enclosed in quotation marks), + number. When the script runs, it will assign the text variable where it is indicated and the number variable where it is indicated as well, to create a sentence that says Many years ago when I was 16.

This now takes us to Arrays. Arrays are also variables, but they are more complex, meaning that they contain more data or values than regular variables do. Arrays simply index values, always starting with the number zero. Here is a sample of an Array, using the days of the week.

var days = new Array()
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
days[3] = "Wednesday";
days[4] = "Thursday;
days[5] = "Friday";
days[6] = "Saturday";

When you create a variable such as this, you are actually creating an Array, and it is defined as a new Array object by putting new Array() as the value of the variable. Arrays can hold as many values as you like. To join two variables together, you use the plus sign.



  JavaScript Tutorial: An Introduction to JavaScript
  The Basics of JavaScript
  JavaScript Events
  JavaScript Variables and Arrays
  JavaScript Statements
  JavaScript Functions and Methods
  JavaScript Errors
  JavaScript Food Chain
  Writing JavaScript Code
  JavaScript Object Reference
  JavaScript Events Reference
  JavaScript Functions Reference
  JavaScript Math Object Reference
  JavaScript Array Object Reference
  JavaScript String Object Reference
  JavaScript Date Object Reference

Web Development Tutorials

  Cascading Style Sheets Tutorial: An Introduction to Cascading Style Sheets
  JavaScript Tutorial: An Introduction to JavaScript
  Web Development: A step by step guide to developing a successful Internet business
  HTML Codes Chart: Copy and paste HTML codes for your web page
  HTML Tips: Copy and paste special effect HTML codes for your web page
  Web Design Tips: Tips, tricks, and special effect codes for your web page
  JavaScript Code Snippets: Copy and paste special effect JavaScript codes for your web page
  216 Web Safe Color Chart: Hexadecimal and RGB Color Codes for your web page
  ASCII Character Codes Chart: American Standard Code for Information Interchange character codes chart