

Variables and Constants
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.
What is Case Sensitive?
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.
Declaring Your Variables
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 pratice.
example of declaring variables:
| VAR num1, str1, num2, str2; |
Variable Types:
In JavaScript, you can have 4 different types of variables in your program. Below is a list and definition for each.
- Integer: All positive and negative numbers, zero is also included.
- String: Strings are considered to be words and sentences within double quotes.
- Floating Point: This type of variable is any number that will contain a decimal.
- 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:
| num1 = 547; //This is an integer variable. str1 = "Welcome to AllSyntax.com"; //This is a string variable. fp1 = 54.7; //This is a floating point variable. boo1 = True; //And here is a boolean variable. |
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:
| document.write("My variable "+num1+" is of type: "+typeof num1+" in this program."); |
This keyword will work for any type of variable in JavaScript.
Displaying Your Output
Now before I go much further, Im 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(); becuase 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.
Concatenation
Another bit of code you might have noticed in the examle 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.)






