In simplest terms, a variable is a container of values. These values can be changed continually during the script execution. For example, if a variable contains the number 2 as value, we can change it (using operators) to contain something else. Further, we have the convenience of referring to a variable using its name.
Once a value is stored in a variable it can be accessed using the variable name. A variable name can consist of alphanumeric characters and the underscore. It cannot begin with a numeral, though. Note that variable names are case sensitive and my_name is not the same as MY_name.
Valid variable names
Invalid variable names
The var keyword explicitly associates a variable to a memory address. This process is called initialization.
var your_name; var total;
Here two variables, your_name and total have been initialized. However, no value has been assigned to these variables. The process of initialization just sets a memory location for a variable.
Click here to test the alert method
Note: The variable in the above alert() method is not surrounded by quotes.
To assign a value to a variable we use the equal to (=) sign. Thus,
var url = "http://www.webdevelopersnotes.com/"; sum_total = 2001;
Sets the value of url to http://www.webdevelopersnotes.com/ and sum_total to 2001.
The eagle-eyed would have noticed the subtle difference in how the two variables are set. In the first statement, the value part is enclosed in quotes while the quotes are missing in the second statement. The reason is that http://www.webdevelopersnotes.com/ is text while 2001 is a number.
Was this confusing? To simplify the explanation, let’s look at the first two data types.
A number data type can be an integer or a floating-point.
Some integer values
Some floating-point values
Are floating-point numbers decimals numbers? Yes (sort of!)
What is the e doing in the last three values? e or E is an exponent and is always followed by an integer exponent value. In simple terms it means X 10 interger exponent.
3.0e10 means 3.0 X 1010 6.023e-23 means 6.023 X 10-23 34e1 means 34 X 101
Thus, floating point numbers can consists of either a decimal number or a very large or small number that is represented using the e notation.
Numeric data types can be manipulated using mathematical operators such as addition, subtraction, multiplication etc. We will look at these very shortly.
Any character or sting or characters enclosed in double or single quotes is a string data type.
So string data can consist solely of numerals? Yes! However, the number behaves as a string data type and not numeric data. For example, “4” + “10” is equal to “410” but 4 + 10 is 14! (Note, the double quotes… more on this later)
Data Types such as string, number etc. have to be treated in a different manner. For example, you can add, subtract, or multiply numbers but multiplying string data (read text) does not make sense.
We will look at this in more detail when discussing operators.
If you have understood this much, you are ready for the nest session.
There is an "Internet Hall of Fame" which celebrates "people who bring the Internet to life". It was started in 2012 and includes pioneers, global connectors and innovators. [more...]