Left Quote    A clever person solves a problem. A wise person avoids it.
- Einstein    
Right Quote
[login] | [Register]

Learning JavaScript Basics

by: bs0d
Page: 2 of 10
(View All)


What is a variable? A variable is an amount of data which can change over time (throughout your program). What is a constant? A constant is an amount of data which will never change. When using variables in JavaScript, here are a few rules to keep in mind:

  • A variable can be letters, digits and the underscore (_).
  • A variable cannot contain spaces.
  • A variable has to begin with a letter or the underscore.
  • Variables are 'case sensitive' in JavaScript.


If you are unfamiliar with the term, "case sensitive" - This means that JavaScript will recognize the difference between upper case, and lower case letters for names of your variable. Like, "HI" is different from "Hi" which is different from "hi". So keep that in mind when you're using variables and referring to them in JavaScript.


In JavaScript, to declare your variables you are going to use the VAR statement. You need to declare your variables at the beginning of your program, however it is not required. It is just considered to be good practice.

Example of declaring variables:


In JavaScript, you can have 4 different types of variables in your program. Below is a list and definition for each.
  1. Integer: All positive and negative numbers, zero is also included.
  2. String: Strings are considered to be words and sentences within double quotes.
  3. Floating Point: This type of variable is any number that will contain a decimal.
  4. Boolean: A Boolean variable is that of which will be used in a conditional statement that will need one of two values, True or False.
here is an example of each variable type:

See, in Javascript the data type will be assigned to a variable when you give it its value. And JavaScript can see the type of value you give it and assign it a data type. JavaScript automatically recognizes the data types of variables. You can check your variable type by using the typeof keyword. Here is how you could use this keyword:

This keyword will work for any type of variable in JavaScript.


Now before I go much further, I'm sure you noticed the document.write(); in the code example above. This is how you will display information, or print it out on the screen. There is also document.writeln(); document.writeln(); differs from just document.write(); because it will take the cursor down to the next line. As where in document.write(); you must include the html tag, <BR> (which is a hard line break) that takes the cursor down to the next line.


Another bit of code you might have noticed in the example above, are the "+" signs. The "+" sign is a concatenation character used to combine strings, numerical variables and numerical constants. In order to combine say, two or more strings or numbers, you will need to use the "+" character.

Now, I believe this tutorial will be better than previous tutorials written because of the following:

Here is a button you can click that will display a javascript document that you should be able to write and understand from this point. You can simply view the source to see the code behind it. (In IE it is VIEW --> SOURCE.)

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

3 Total Comments
4.66 Rating of 5 (3 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· Getting Started with Ajax
Your Article Here

"" Copyright © 2002-2020; All rights lefted, all lefts righted.
Privacy Policy